diff --git a/.gitignore b/.gitignore
index 2f606647..2c6e06c1 100644
--- a/.gitignore
+++ b/.gitignore
@@ -3,13 +3,20 @@ dist/
coverage/
.DS_Store
reports/
-docs/*
+
+package-lock.json
# Ignore specific dotfiles/folders
.codemachine
+.codemachine/memory
.env
.env.example
.npmrc
# Ignore folders starting with .tmp
-.tmp*
\ No newline at end of file
+.tmp*
+.vscode
+.codemachine/logs
+.codemachine/prompts
+.codemachine/agents
+.codemachine/template.json
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
new file mode 100644
index 00000000..2fb1ab88
--- /dev/null
+++ b/CONTRIBUTING.md
@@ -0,0 +1,104 @@
+# Contributing to CodeMachine
+
+First off, thank you for considering contributing to CodeMachine! It's people like you that make CodeMachine such a great tool.
+
+We welcome contributions in various forms, including but not limited to:
+- New features and enhancements
+- Bug fixes
+- Documentation improvements
+- New templates and workflows
+
+This document focuses on creating new templates and workflows.
+
+## Getting Started
+
+1. **Fork the repository** on GitHub.
+2. **Clone your fork** to your local machine.
+3. **Install the dependencies**:
+ ```bash
+ npm install
+ ```
+4. **Build the project**:
+ ```bash
+ npm run build
+ ```
+5. **Link the package**: To test your changes globally, use `npm link`:
+ ```bash
+ npm link
+ ```
+
+## The Config Directory
+
+The `config` directory is the heart of CodeMachine's configuration. It's where you define agents, modules, and placeholders that can be used in your workflows and templates. Understanding this directory is crucial for customizing and extending CodeMachine.
+
+Here's a breakdown of the files in the `config` directory:
+
+- `main.agents.js`: Defines the main agents that can be used as steps in a workflow.
+- `sub.agents.js`: Defines the sub-agents that can be used for parallel execution within a workflow.
+- `modules.js`: Defines special modules that can be used as steps in a workflow. Modules can have custom behaviors, such as looping.
+- `placeholders.js`: Defines placeholders that can be used in your prompt templates. This allows you to inject dynamic content into your prompts.
+
+By modifying these files, you can add new agents, create custom modules, and define your own placeholders to tailor CodeMachine to your specific needs.
+
+
+
+Prompt templates are essential for guiding the behavior of the AI agents. They are located in the `prompts/templates` directory.
+
+### Template Structure
+
+The `prompts/templates` directory is organized into subdirectories, each representing a different template set. For example, the `codemachine` directory contains the templates for the core CodeMachine workflow.
+
+Each template is a Markdown file (`.md`) that defines the prompt for a specific agent. The file name corresponds to the agent's ID. For example, `01-architecture-agent.md` is the template for the `architecture-agent`.
+
+### Creating a New Template
+
+1. **Choose a template set**: Decide which template set your new template belongs to, or create a new directory if needed.
+2. **Create a new Markdown file**: The file name should be descriptive of the agent's purpose.
+3. **Write the template**: The content of the file should be a clear and concise prompt that guides the agent's behavior. You can use placeholders in the format `{{placeholder_name}}` to insert dynamic content. The available placeholders are defined in the `config/placeholders.js` file.
+
+## Creating Workflows
+
+Workflows define the sequence of steps and agents that are executed to achieve a specific goal. They are located in the `templates/workflows` directory.
+
+### Workflow Structure
+
+A workflow is a JavaScript file that exports a workflow definition object. The object has the following properties:
+
+- `name`: The name of the workflow.
+- `steps`: An array of steps to be executed in sequence.
+- `subAgentIds`: An array of sub-agent IDs that can be used in the workflow.
+
+### Creating a New Workflow
+
+1. **Create a new JavaScript file** in the `templates/workflows` directory. The file name should be descriptive of the workflow's purpose (e.g., `my-new-workflow.workflow.js`).
+2. **Define the workflow object**: Create a JavaScript object with the `name`, `steps`, and `subAgentIds` properties.
+3. **Define the steps**: Each step is an object that defines the agent to be executed and its parameters. You can use the `resolveStep`, `resolveModule`, and `resolveFolder` functions to create steps. These functions load agents, modules, and folders defined in the `config` directory.
+
+ - `resolveStep(agentId, parameters)`: Loads a main agent defined in `config/main.agents.js`.
+ - `resolveModule(moduleId, parameters)`: Loads a module defined in `config/modules.js`.
+ - `resolveFolder(folderId, parameters)`: Loads a folder of agents defined in `config/main.agents.js`. This is useful for applying the same parameters to a group of agents.
+4. **Add sub-agents**: If your workflow uses sub-agents, add their IDs to the `subAgentIds` array.
+
+### Example Workflow
+
+Here is an example of a simple workflow:
+
+```javascript
+export default {
+ name: 'My New Workflow',
+ steps: [
+ resolveStep('my-agent', { /* parameters */ }),
+ resolveModule('my-module', { /* parameters */ }),
+ ],
+ subAgentIds: ['my-sub-agent'],
+};
+```
+
+## Submitting Changes
+
+1. **Create a new branch** for your changes.
+2. **Commit your changes** with a clear and descriptive commit message.
+3. **Push your changes** to your fork on GitHub.
+4. **Create a pull request** to the main repository.
+
+We will review your pull request as soon as possible. Thank you for your contribution!
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 00000000..261eeb9e
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,201 @@
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+ 1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+ 2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+ 3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+ 4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+ 6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+ 7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+ 8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+ 9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+ END OF TERMS AND CONDITIONS
+
+ APPENDIX: How to apply the Apache License to your work.
+
+ To apply the Apache License to your work, attach the following
+ boilerplate notice, with the fields enclosed by brackets "[]"
+ replaced with your own identifying information. (Don't include
+ the brackets!) The text should be enclosed in the appropriate
+ comment syntax for the file format. We also recommend that a
+ file or class name and description of purpose be included on the
+ same "printed page" as the copyright notice for easier
+ identification within third-party archives.
+
+ Copyright [yyyy] [name of copyright owner]
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
diff --git a/README.md b/README.md
index a1e1e925..4550e001 100644
--- a/README.md
+++ b/README.md
@@ -1,71 +1,81 @@
+
-
+ npm i -g codemachine
-## Multi-Agent Workflow Orchestration
-> **✨ CodeMachine Built Itself**
->
-> **90% of this entire codebase was generated by CodeMachine from a single specification file.**
-> This isn't a demo—it's proof. CodeMachine orchestrated its own architecture, planning, implementation, and testing.
+
+ CodeMachine CLI is an autonomous multi-agent platform that works locally on your computer, turning specifications into production-ready code.
-
+
-### What is CodeMachine CLI?
-CodeMachine CLI is a multi-agent workflow orchestration framework that transforms project specifications into production-ready codebases. You feed it a spec, and CodeMachine orchestrates top-tier agents like Claude CLI, Codex CLI, and Cursor AI end-to-end to deliver complex builds while you define the workflow. It also coordinates additional providers (Gemini CLI, Qwen Coder) in parallel so each agent tackles the slice it does best.
+
+ ✨ CodeMachine Built Itself
+
-**Why CodeMachine?**
+
+ 90% of this entire codebase was generated by CodeMachine from a single specification file.
+ This isn't a demo—it's proof. CodeMachine engine orchestrated its own architecture, planning, implementation, and testing—creating a massively scalable codebase ready for continuous updates and improvements.
+
-- **🔄 Custom Workflows**: Design orchestration workflows for any complexity—from simple scripts to multi-day development cycles
-- **🤝 Multi-Agent Collaboration**: Mix AI providers strategically (Gemini plans, Claude builds, Codex reviews)
-- **⚡ Parallel Execution**: Sub-agents work simultaneously for 10x faster output
-- **🎯 Endless Orchestration**: Workflows run for hours or days to achieve complex goals automatically
---
-## 🚀 Quick Start
+## **What is CodeMachine?**
+
+CodeMachine is a CLI-native orchestration platform that transforms specification files and contextual inputs into production-ready code through coordinated multi-agent workflows. Specialized AI agents operate in hierarchical and parallel configurations with the ability for bidirectional communication, enabling runtime-adaptable methodologies that dynamically adjust to project requirements without framework modifications.
+
-Get CodeMachine up and running in minutes:
+**Why CodeMachine?**
-### 1. Install CodeMachine
+* **Customizable, End-to-End Workflows:** Architect sophisticated orchestration pipelines for any scale, from executing simple scripts to managing multi-day, complex development cycles.
+* **Strategic Multi-Agent Collaboration:** Leverage a heterogeneous multi-agent system by assigning specialized models to specific tasks—for instance, using Gemini for planning, Claude for implementation, and another model for code review.
+* **Massively Parallel Execution:** Achieve significantly accelerated output by deploying sub-agents that operate simultaneously on different components of a task.
+* **Persistent, Long-Running Orchestration:** Execute workflows for extended durations—hours or even days—to autonomously accomplish complex, long-term development goals.
+---
+## 🚀 Quick Start
+
+### **Installing and running CodeMachine CLI**
+
+First, install the command-line tool globally via npm:
```bash
npm install -g codemachine
```
-### 2. Run CodeMachine
-
-Navigate to your project directory and run:
-
+Then, simply run `codemachine` in your project directory to get started.
```bash
-cd ~/your-project-directory
codemachine
```
+### **Initializing a Project**
-On first run, CodeMachine creates a `.codemachine/` folder. Write your project requirements in `.codemachine/inputs/specifications.md`, choose a workflow template, and watch as CodeMachine orchestrates specialized agents to:
-- Design your system architecture
-- Generate implementation plans
-- Write production-ready code
-- Create automation scripts
-- Validate outputs against your specs
+ CodeMachine initializes a `.codemachine/` workspace. To start **add your specs** to the `inputs/specifications.md` file, then **run `/start`** and watch the magic happen, CodeMachine will:
+ * **Architect a complete system blueprint from your requirements.**
+ * **Formulate detailed, step-by-step execution plans.**
+ * **Engineer clean, production-grade code for every component.**
+ * **Generate essential automation for testing and deployment.**
+ * **Integrate rigorous validation checks across every phase of execution.**
### Supported AI Engines
+CodeMachine requires at least one CLI-based AI engine to handle the primary roles of planning and writing code, and is designed to orchestrate multiple engines to collaborate within a single workflow. The table below shows the current status of supported engines and their platform compatibility.
+
+
| CLI Engine |
Status |
- Main Agents |
- Sub Agents |
- Orchestrate |
+ Windows |
+ macOS |
+ Linux |
| Codex CLI |
✅ Supported |
- ✅ |
+ ⚠️ |
✅ |
✅ |
@@ -77,112 +87,264 @@ On first run, CodeMachine creates a `.codemachine/` folder. Write your project r
✅ |
- | Cursor CLI |
+ CCR (Claude Code Router) |
+ ✅ Supported |
+ ✅ |
+ ✅ |
+ ✅ |
+
+
+ | OpenCode CLI |
✅ Supported |
✅ |
✅ |
✅ |
+
+ | Cursor CLI |
+ ✅ Supported |
+ ❌ |
+ ✅ |
+ ✅ |
+
| Gemini CLI |
🚧 Coming Soon |
- 🚧 |
- 🚧 |
- 🚧 |
+ ✅ |
+ ✅ |
+ ✅ |
| Qwen Coder |
🚧 Coming Soon |
- 🚧 |
- 🚧 |
- 🚧 |
+ ✅ |
+ ✅ |
+ ✅ |
-### Requirements
+
+ ✅ Fully Supported | ⚠️ Not Officially Supported | ❌ Not Available
+
+
+### OpenCode CLI Integration
+
+OpenCode ships as a first-class engine. Install the CLI with `npm i -g opencode-ai@latest` (or `brew install opencode`, `scoop install extras/opencode`, `choco install opencode`) and then:
+
+- `codemachine opencode run "build hello world"` streams JSON-formatted OpenCode output through CodeMachine’s log markers.
+- Workflow steps can force OpenCode with `codemachine step --engine opencode --model anthropic/claude-3.7-sonnet`.
+- Guardrail environment defaults (overridable) are applied automatically:
+ `OPENCODE_PERMISSION={"edit":"allow","webfetch":"allow","bash":{"*":"allow"}}`,
+ `OPENCODE_DISABLE_LSP_DOWNLOAD=1`, `OPENCODE_DISABLE_DEFAULT_PLUGINS=1`, and `OPENCODE_CONFIG_DIR=$HOME/.codemachine/opencode`.
+- Set `CODEMACHINE_SKIP_OPENCODE=1` for dry-run workflows or `CODEMACHINE_PLAIN_LOGS=1` when you need ANSI-free logs.
-- Node.js ≥ 20.10.0 and npm ≥ 9 (pnpm works too)
-- From source: `pnpm install && pnpm build` (try `pnpm test` to verify the workspace)
+---
+
+## **Running on VPS / Remote Servers**
+
+CodeMachine can run on remote servers for long-running workflows. Special handling is provided for SSH disconnections and headless operation.
-### Core CLI Commands
+### Quick Start
-(`codemachine --help` for options)
-- `codemachine templates` — list and activate workflow templates
-- `codemachine agent "prompt"` — run a specific agent once; supports `--profile` and `--model`
+```bash
+# Launch persistently (survives SSH disconnect)
+./scripts/deployment/codemachine-persistent.sh
+
+# Close laptop / disconnect SSH - process continues
-### Workflow & Specs
+# Reconnect and check status
+./scripts/deployment/codemachine-status.sh
-- Specs live in `.codemachine/inputs/specifications.md` once you bootstrap
-- Workflow templates reside in `templates/workflows/`; pick or extend them with `codemachine templates`
-- Generated agents and run state land in `.codemachine/`; inspect logs under `.codemachine/runtime`
-- Deep dives: [docs/CLI-ENGINES.md](docs/CLI-ENGINES.md), [docs/operations](docs/operations), and [docs/architecture](docs/architecture)
+# Watch logs
+tail -f .codemachine/logs/persistent-output.log
+```
-**That's it!** Your complete codebase will be generated and ready to ship.
+### Understanding UI Behavior
+
+When you reconnect via SSH after disconnection:
+- ✅ **Process continues running** (verified by status script)
+- ❌ **Interactive UI detaches** (expected - Ink framework limitation)
+
+**This is not a bug.** The compute layer (agents, code generation) works correctly in headless mode. Use the status script and logs for monitoring.
+
+### Available Tools
+
+- `codemachine-persistent.sh` - Launch with SSH disconnect survival
+- `codemachine-status.sh` - Enhanced monitoring (running agents, logs, errors)
+- `codemachine-stop.sh` - Graceful shutdown
+
+📖 **Full guide:** [docs/vps-deployment.md](docs/vps-deployment.md)
---
-## 🎮 Example: Snake Game
+## **Production Validation:**
-CodeMachine handles complex, large-scale projects. We created this simple showcase to demonstrate the workflow.
+CodeMachine has been battle-tested on the Sustaina Platform a full-stack ESG compliance system spanning **7 microservices**, **500+ files**, and **60,000+ lines of code** across Python, TypeScript, React, FastAPI, and NestJS.
-We wrote a simple specification for a classic snake game:
+
+
+ | Services Generated |
+ 7 microservices (AI/ML + CRUD APIs) |
+
+
+ | Codebase Scale |
+ ~500 files, 60K+ Line of code |
+
+
+ | Tech Stack |
+ React 18, FastAPI, NestJS, PostgreSQL, MongoDB, Redis, Kubernetes |
+
+
+ | Time to MVP |
+ ~8 hours of autonomous orchestration |
+
+
-```markdown
-# Snake Game Specification
+### **CodeMachine vs Regular AI Agents**
-## Goal
-Create a classic snake game that runs in the terminal or browser.
+We conducted a real-world comparison by monitoring development work on a project of identical scope and complexity using the most powerful AI agent tools (Claude Code, Cursor, Copilot) with manual orchestration and human review, versus CodeMachine's autonomous multi-agent orchestration.
-## Core Features
-- Snake moves continuously in a direction (up, down, left, right)
-- Player controls direction using arrow keys or WASD
-- Snake grows longer when eating food
-- Food spawns randomly on the grid
-- Game ends when snake hits wall or itself
-- Display current score (length of snake)
+
+
+ | Aspect |
+ Regular AI Agents (Manual Orchestration + Human Review) |
+ CodeMachine (Autonomous Orchestration) |
+
+
+ | Architecture Planning |
+ 4-6 hours of manual prompting |
+ Automated (30 min) |
+
+
+ | Service Implementation |
+ 140-200 hours (7 services × 20-30h each) Manual prompting, context switching |
+ Parallel execution (5 hours) |
+
+
+ | Integration & Testing |
+ 30-50 hours Manual coordination, debugging |
+ Automated validation (2 hours) |
+
+
+ | Deployment Setup |
+ 8-12 hours Scripts, configs, orchestration |
+ Auto-generated (30 min) |
+
+
+ | Code Consistency |
+ Inconsistent patterns across services Different coding styles per session |
+ Unified architecture & patterns Consistent across all components |
+
+
+ | Quality Control |
+ Manual review required Errors compound over time |
+ Built-in validation at each step Automated sanity checks |
+
+
+ | Context Retention |
+ Lost between sessions Repeated explanations needed |
+ Full project context maintained Cross-service awareness |
+
+
+ | Total Developer Time |
+ ~200-300 hours |
+ ~8 hours |
+
+
+ | Efficiency Gain |
+ Baseline |
+ 25-37× faster |
+
+
-## Technical Requirements
-- Game grid: 20x20 cells
-- Initial snake length: 3 segments
-- Game speed: 100-200ms per frame
-```
+Real-world comparison: One developer manually prompting AI coding assistants vs CodeMachine's autonomous multi-agent orchestration
+
+---
+
+
+ Want to see how CodeMachine built this?
+ Explore the complete case study showing the detailed path CodeMachine took to create this project—every step, decision, and workflow tracked from specification to production.
+
-
+ 📊 View Complete Case Study & Development Track →
-**The comparison speaks for itself:** GPT-5 Codex (High) alone vs. CodeMachine's default workflow powered by GPT-5 Codex (Low). The quality gap is massive. Multi-agent workflow orchestration delivers production-ready results that far exceed what single-agent generation can achieve—even with a more powerful model.
+---
-**[See the full Snake Game implementation →](https://github.com/moazbuilds/Snake-game-codemachine)**
-Explore the complete project structure, git commit history showing CodeMachine's step-by-step workflow, and both stunning terminal and web versions generated from a single specification.
+## 📚 Documentation
+
+**Getting Started**
+- [Prerequisites & Installation](docs/architecture.md#prerequisites)
+- [Quick Start Guide](docs/architecture.md#get-your-first-project-generated)
+ - [Writing Your Specification](docs/specification-schema.md#part-1-the-essentials-core-requirements-for-any-project)
+ - [Running the Workflow](docs/architecture.md#get-your-first-project-generated)
+- [How CodeMachine Works](docs/architecture.md#how-codemachine-works)
+
+**Core Concepts**
+- [Agents in CodeMachine](docs/architecture.md#agents-in-codemachine)
+ - [Main Agents](docs/architecture.md#main-agents)
+ - [Sub Agents](docs/architecture.md#sub-agents)
+ - [Modules](docs/architecture.md#modules)
+ - [Dynamic Agent Generation](docs/architecture.md#dynamic-agent-generation)
+- [Communication Patterns](docs/architecture.md#agent-communication-patterns)
+ - [Sequential Execution](docs/architecture.md#1-sequential-hierarchical-communication)
+ - [Parent-Child Delegation](docs/architecture.md#2-parent-child-agent-to-agent-communication)
+- [Context Management](docs/architecture.md#agent-context-management-types)
+ - [File-Based Memory](docs/architecture.md#1-file-based-main-agent-memory)
+ - [Session Memory](docs/architecture.md#2-orchestrator-agent-session-memory)
+
+**CLI Usage**
+- [CLI Overview](docs/cli-reference.md#overview)
+ - [Global Options](docs/cli-reference.md#overview)
+ - [Interactive Mode](docs/cli-reference.md#interactive-mode)
+- [Workflow Commands](docs/cli-reference.md#workflow-commands)
+ - [Start Command](docs/cli-reference.md#start)
+ - [Template Selection](docs/cli-reference.md#templates)
+- [Development Commands](docs/cli-reference.md#development-commands)
+ - [Run Command](docs/cli-reference.md#run)
+ - [Step Execution](docs/cli-reference.md#step)
+- [Authentication](docs/cli-reference.md#auth)
+ - [Login](docs/cli-reference.md#auth-login)
+ - [Logout](docs/cli-reference.md#auth-logout)
+- [Advanced Topics](docs/cli-reference.md#advanced-topics)
+ - [Engine-Specific Commands](docs/cli-reference.md#engine-specific-commands)
+
+**Creating Custom Workflows**
+- [Workflow Templates](docs/customizing-workflows.md#workflow-templates)
+ - [Template Structure](docs/customizing-workflows.md#template-structure)
+ - [Step Resolution Functions](docs/customizing-workflows.md#step-resolution-functions)
+ - [Override Options](docs/customizing-workflows.md#complete-override-options-reference)
+- [Configuring Agents](docs/customizing-workflows.md#configuration-files)
+ - [Main Agents](docs/customizing-workflows.md#main-agents-configuration)
+ - [Sub Agents](docs/customizing-workflows.md#sub-agents-configuration)
+ - [Workflow Modules](docs/customizing-workflows.md#workflow-modules-configuration)
+- [Engine & Model Selection](docs/customizing-workflows.md#engine--model-configuration)
+ - [Available Engines](docs/customizing-workflows.md#available-engines)
+ - [Model Options](docs/customizing-workflows.md#model-options)
+ - [Reasoning Levels](docs/customizing-workflows.md#reasoning-effort-levels)
+- [Advanced Patterns](docs/customizing-workflows.md#advanced-workflow-patterns)
+ - [Loop Behaviors](docs/customizing-workflows.md#workflow-modules-configuration)
+ - [Fallback Handling](docs/customizing-workflows.md#complete-override-options-reference)
+ - [Mixed Engine Workflows](docs/customizing-workflows.md#engine--model-configuration)
+
+**Writing Specifications**
+- [Specification Schema](docs/specification-schema.md)
+ - [Essential Requirements](docs/specification-schema.md#part-1-the-essentials-core-requirements-for-any-project)
+ - [Advanced Specifications](docs/specification-schema.md#part-2-advanced-specifications-for-complex-or-high-fidelity-projects)
---
-## 🛠️ How It Works
+## 🙏 Contributors
-
-
-
+Special thanks to the following contributors who have helped make CodeMachine better:
+
+- **[Bahy Ali](https://github.com/bahyali)** - Architect of the original workflow system and core orchestration concepts. His deep expertise and guidance were instrumental in shaping CodeMachine's foundation.
+
+- **[Adinda Praditya](https://github.com/apraditya)** - Added CCR (Claude Code Router) engine support, removing a major limitation by enabling users to leverage AI capabilities beyond subscription-based services.
+
+- **[SoyHub](https://github.com/SoyHub)** - Enhanced the UI system and contributed innovative ideas during brainstorming sessions that helped strengthen CodeMachine's capabilities.
+
+- **[TheMightyDman](https://github.com/TheMightyDman)** - Added OpenCode CLI engine integration, which brings support for multiple AI providers (Anthropic, OpenAI, Google, and more) to CodeMachine. An enthusiastic and active contributor to the project.
-CodeMachine orchestrates workflows through sequential main agent steps and parallel sub-agent execution. After selecting a workflow template, the main agent processes each step in order. When sub-agents are triggered, they work simultaneously on specialized tasks (e.g., frontend, backend, database), then results flow back into the main workflow. Conditional loops allow workflows to iterate until completion criteria are met.
-
-**Example Workflow:**
-
-```javascript
-export default {
- name: 'E-Commerce Platform Builder',
- steps: [
- resolveStep('arch-agent', { engine: 'claude', model: 'sonnet' }), // Claude designs system architecture
- resolveStep('plan-agent', { engine: 'claude', model: 'sonnet' }), // Claude creates detailed implementation plan
- resolveStep('code-generation', { engine: 'codex', model: 'gpt-5', modelReasoningEffort: 'low' }), // Codex generates implementation
- resolveStep('runtime-prep', { engine: 'codex', model: 'gpt-5', modelReasoningEffort: 'low' }), // Codex creates automation scripts
- resolveStep('quality-check', { engine: 'cursor' }), // Codex reviews code quality and runtime
- resolveModule('check-task', { // Loop until all tasks complete
- loopTrigger: 'TASKS_COMPLETED=FALSE',
- loopMaxIterations: 15
- }),
- ],
- subAgentIds: ['payment-agent', 'inventory-agent', 'auth-agent'], // Sub-agents work in parallel
-};
-```
---
diff --git a/assets/images/diagram1.jpg b/assets/images/diagram1.jpg
deleted file mode 100644
index 73314a56..00000000
Binary files a/assets/images/diagram1.jpg and /dev/null differ
diff --git a/assets/images/snake_game.gif b/assets/images/snake_game.gif
deleted file mode 100644
index 1b7d7761..00000000
Binary files a/assets/images/snake_game.gif and /dev/null differ
diff --git a/config/main.agents.js b/config/main.agents.js
index 48cf3485..b6f405e0 100644
--- a/config/main.agents.js
+++ b/config/main.agents.js
@@ -3,76 +3,126 @@ const path = require('node:path');
const promptsDir = path.join(__dirname, '..', 'prompts', 'templates');
module.exports = [
- // Smart Build agents
- {
- id: 'agents-builder',
- name: 'Agent Builder',
- description: 'Generates specialized agent prompts tailored to the current workspace context',
- promptPath: path.join(promptsDir, 'smart-build', '0- agents-builder.md'),
- },
- {
- id: 'project-manager',
- name: 'Project Manager',
- description: 'Prioritizes and sequences tasks, delegating work to other agents as needed',
- promptPath: path.join(promptsDir, 'smart-build', '1- project-manager.md'),
- },
// Codemachine agents
{
id: 'arch-agent',
name: 'Architecture Agent',
description: 'Defines system architecture and technical design decisions',
- promptPath: path.join(promptsDir, 'codemachine', '0- arch-agent.md'),
+ promptPath: path.join(promptsDir, 'codemachine', 'agents', '01-architecture-agent.md'),
},
{
id: 'plan-agent',
name: 'Plan Agent',
description: 'Analyzes requirements and generates comprehensive iterative development plans with architectural artifacts',
- promptPath: path.join(promptsDir, 'codemachine', '1- plan-agent.md'),
+ promptPath: path.join(promptsDir, 'codemachine', 'agents', '02-planning-agent.md'),
},
{
id: 'task-breakdown',
name: 'Task Breakdown Agent',
description: 'Extracts and structures tasks from project plans into JSON format',
- promptPath: path.join(promptsDir, 'codemachine', '2- task-breakdown.md'),
+ promptPath: path.join(promptsDir, 'codemachine', 'agents', '03-task-breakdown-agent.md'),
+ },
+ {
+ id: 'context-manager',
+ name: 'Context Manager Agent',
+ description: 'Gathers and prepares relevant context from architecture, plan, and codebase for task execution',
+ promptPath: path.join(promptsDir, 'codemachine', 'agents', '04-context-manager-agent.md'),
},
{
id: 'code-generation',
name: 'Code Generation Agent',
description: 'Generates code implementation based on task specifications and design artifacts',
- promptPath: path.join(promptsDir, 'codemachine', '3- code-generation.md'),
+ promptPath: path.join(promptsDir, 'codemachine', 'agents', '05-code-generation-agent.md'),
},
{
id: 'task-sanity-check',
name: 'Task Verification Agent',
description: 'Verifies generated code against task requirements and acceptance criteria',
- promptPath: path.join(promptsDir, 'codemachine', '4- task-sanity-check.md'),
+ promptPath: path.join(promptsDir, 'codemachine', 'agents', '06-task-validation-agent.md'),
},
{
id: 'runtime-prep',
name: 'Runtime Preparation Agent',
description: 'Generates robust shell scripts for project automation (install, run, lint, test)',
- promptPath: path.join(promptsDir, 'codemachine', '5- runtime-perp.md'),
+ promptPath: path.join(promptsDir, 'codemachine', 'agents', '07-runtime-preparation-agent.md'),
},
{
id: 'git-commit',
name: 'Git Commit Agent',
description: 'Handles git commit operations and commit message generation',
- promptPath: path.join(promptsDir, 'codemachine', 'git-commit.md'),
+ promptPath: path.join(promptsDir, 'codemachine', 'workflows', 'git-commit-workflow.md'),
+ },
+ {
+ id: 'cleanup-code-fallback',
+ name: 'Cleanup Code Fallback File',
+ description: 'Deletes .codemachine/prompts/code_fallback.md if it exists',
+ promptPath: path.join(promptsDir, 'codemachine', 'workflows', 'cleanup-code-fallback-workflow.md'),
+ },
+ {
+ id: 'plan-fallback',
+ name: 'Plan Fallback Agent',
+ description: 'Fixes and validates plan generation issues when plan-agent fails',
+ promptPath: path.join(promptsDir, 'codemachine', 'fallback-agents', 'planning-fallback.md'),
+ },
+ {
+ id: 'task-fallback',
+ name: 'Task Fallback Agent',
+ description: 'Fixes and validates task breakdown issues when task-breakdown fails',
+ promptPath: path.join(promptsDir, 'codemachine', 'fallback-agents', 'task-breakdown-fallback.md'),
},
- // Folder configurations - applies settings to all agents in the folder
+ // Test agents
{
- type: 'folder',
- id: 'codemachine',
- name: 'Codemachine',
- description: 'Core codemachine workflow agents',
- folderPath: path.join(promptsDir, 'codemachine'),
+ id: 'test-agent-1',
+ name: 'Test Agent 1',
+ description: 'First test agent for workflow testing',
+ promptPath: path.join(promptsDir, 'test-workflows', 'test-agent-1.md'),
},
{
- type: 'folder',
- id: 'spec-kit',
- name: 'Spec Kit',
- description: 'Specification and planning agents for project setup',
- folderPath: path.join(promptsDir, 'spec-kit'),
+ id: 'test-agent-2',
+ name: 'Test Agent 2',
+ description: 'Second test agent for workflow testing',
+ promptPath: path.join(promptsDir, 'test-workflows', 'test-agent-2.md'),
},
+ {
+ id: 'test-agent-3',
+ name: 'Test Agent 3',
+ description: 'Third test agent for workflow testing',
+ promptPath: path.join(promptsDir, 'test-workflows', 'test-agent-3.md'),
+ },
+
+ // Dev codemachine agents
+ {
+ id: 'init',
+ name: 'Init',
+ description: 'Initializes codemachine development environment (creates branch and updates .gitignore)',
+ promptPath: path.join(promptsDir, 'dev-codemachine', 'main-agents', '00-init.md'),
+ },
+ {
+ id: 'principal-analyst',
+ name: 'Principal Analyst - Checkpoint',
+ description: 'Reviews project specifications and identifies critical ambiguities requiring clarification',
+ promptPath: path.join(promptsDir, 'dev-codemachine', 'main-agents', '01-principal-analyst.md'),
+ },
+ {
+ id: 'specifications-indexer',
+ name: 'Specifications Indexer',
+ description: 'Indexes and structures project specifications for efficient access and reference',
+ promptPath: path.join(promptsDir, 'dev-codemachine', 'main-agents', '02-specifications-indexer.md'),
+ },
+ {
+ id: 'blueprint-orchestrator',
+ name: 'Blueprint Orchestrator',
+ description: 'Orchestrates the execution of Foundation, Structural-Data, Behavior, and Ops-Docs architects with resilience and resumability',
+ promptPath: path.join(promptsDir, 'dev-codemachine', 'main-agents', '04-blueprint-orchestrator.md'),
+ },
+
+ // Folder configurations - applies settings to all agents in the folder
+ //{
+ // type: 'folder',
+ // id: 'codemachine',
+ // name: 'Codemachine',
+ // description: 'Core codemachine workflow agents',
+ // folderPath: path.join(promptsDir, 'codemachine'),
+ //},
];
diff --git a/config/modules.js b/config/modules.js
index 4ac075b1..66386a48 100644
--- a/config/modules.js
+++ b/config/modules.js
@@ -5,9 +5,30 @@ const promptsDir = path.join(__dirname, '..', 'prompts');
module.exports = [
{
id: 'check-task',
- name: 'Check Task',
+ name: 'Task Completion Checker',
description: 'Validates that all tasks are completed and signals whether to repeat workflow steps.',
- promptPath: path.join(promptsDir, 'templates', 'codemachine', 'tasks-checker.md'),
+ promptPath: path.join(promptsDir, 'templates', 'codemachine', 'workflows', 'task-verification-workflow.md'),
+ behavior: {
+ type: 'loop',
+ action: 'stepBack',
+ },
+ },
+ {
+ id: 'iteration-checker',
+ name: 'Iteration Checker',
+ description: 'Checks if additional iterations are needed and can trigger other agents dynamically.',
+ promptPath: path.join(promptsDir, 'templates', 'codemachine', 'workflows', 'iteration-verification-workflow.md'),
+ behavior: {
+ type: 'trigger',
+ action: 'mainAgentCall',
+ triggerAgentId: 'git-commit', // Default agent to trigger, can be overridden by behavior.json
+ },
+ },
+ {
+ id: 'auto-loop',
+ name: 'Auto Loop',
+ description: 'Simple auto loop module for testing - always signals to continue looping.',
+ promptPath: path.join(promptsDir, 'templates', 'test-workflows', 'auto-loop.md'),
behavior: {
type: 'loop',
action: 'stepBack',
diff --git a/config/placeholders.js b/config/placeholders.js
index fb6c3279..945e973d 100644
--- a/config/placeholders.js
+++ b/config/placeholders.js
@@ -1,18 +1,36 @@
+const path = require('node:path');
module.exports = {
// Paths relative to user's project directory
userDir: {
// Project specification document
- specifications: '.codemachine/inputs/specifications.md',
- architecture: '.codemachine/plan/architecture.md',
- plan: '.codemachine/plan/plan.md',
- tasks: '.codemachine/plan/tasks.json',
+ specifications: path.join('.codemachine', 'inputs', 'specifications.md'),
+ architecture: path.join('.codemachine', 'artifacts', 'architecture', '*.md'),
+ architecture_manifest_json: path.join('.codemachine', 'artifacts', 'architecture', 'architecture_manifest.json'),
+ foundation: path.join('.codemachine', 'artifacts', 'architecture', '01_Blueprint_Foundation.md'),
+ plan: path.join('.codemachine', 'artifacts', 'plan', '*.md'),
+ plan_manifest_json: path.join('.codemachine', 'artifacts', 'plan', 'plan_manifest.json'),
+ plan_fallback: path.join('.codemachine', 'prompts', 'plan_fallback.md'),
+ tasks: path.join('.codemachine', 'artifacts', 'tasks.json'),
+ all_tasks_json: path.join('.codemachine', 'artifacts', 'tasks', '*.json'),
+ task_fallback: path.join('.codemachine', 'prompts', 'task_fallback.md'),
+ context: path.join('.codemachine', 'prompts', 'context.md'),
+ code_fallback: path.join('.codemachine', 'prompts', 'code_fallback.md'),
// Add more placeholders as needed:
},
// Paths relative to codemachine package root
packageDir: {
- orchestration_guide: 'prompts/orchestration/guide.md',
+ orchestration_guide: path.join('prompts', 'orchestration', 'guide.md'),
+ arch_output_format: path.join('prompts', 'templates', 'codemachine', 'output-formats', 'architecture-output.md'),
+ plan_output_format: path.join('prompts', 'templates', 'codemachine', 'output-formats', 'planning-output.md'),
+ task_output_format: path.join('prompts', 'templates', 'codemachine', 'output-formats', 'task-breakdown-output.md'),
+ context_output_format: path.join('prompts', 'templates', 'codemachine', 'output-formats', 'context-output.md'),
+ task_validation_output_format: path.join('prompts', 'templates', 'codemachine', 'output-formats', 'task-validation-output.md'),
+ // dev.codemachine
+ smart_anchor: path.join('prompts', 'templates', 'dev-codemachine', 'sub-agents', 'shared-instructions', 'smart-anchor.md'),
+ command_constraints: path.join('prompts', 'templates', 'dev-codemachine', 'sub-agents', 'shared-instructions', 'command-constraints.md'),
+ atomic_generation: path.join('prompts', 'templates', 'dev-codemachine', 'sub-agents', 'shared-instructions', 'atomic-generation.md'),
// Add codemachine package-level placeholders here
}
};
diff --git a/config/sub.agents.js b/config/sub.agents.js
index b5f7bf93..49e5e4cb 100644
--- a/config/sub.agents.js
+++ b/config/sub.agents.js
@@ -1,53 +1,85 @@
-const path = require('node:path');
-
module.exports = [
{
id: 'uxui-designer',
name: 'UX/UI Designer',
description: 'Handle UX and UI design tasks',
- promptPath: path.join(__dirname, '..', 'prompts', 'ux-ui-designer.md'),
},
{
id: 'frontend-dev',
name: 'Frontend Developer',
description: 'Handle frontend development tasks',
- promptPath: path.join(__dirname, '..', 'prompts', 'frontend-developer.md'),
},
{
id: 'backend-dev',
name: 'Backend Developer',
description: 'Handle backend development tasks',
- promptPath: path.join(__dirname, '..', 'prompts', 'backend-developer.md'),
},
{
id: 'solution-architect',
name: 'Solution Architect',
description: 'Handle solution architecture tasks',
- promptPath: path.join(__dirname, '..', 'prompts', 'solution-architect.md'),
},
{
id: 'technical-writer',
name: 'Technical Writer / Documentation Specialist',
description: 'Handle documentation and writing tasks',
- promptPath: path.join(__dirname, '..', 'prompts', 'technical-writer.md'),
},
{
id: 'qa-engineer',
name: 'QA/Test Engineer',
description: 'Handle testing and QA tasks',
- promptPath: path.join(__dirname, '..', 'prompts', 'qa-test-engineer.md'),
},
{
id: 'performance-engineer',
name: 'Performance Engineer',
description: 'Handle performance profiling and optimization tasks',
- promptPath: path.join(__dirname, '..', 'prompts', 'performance-engineer.md'),
},
{
id: 'software-architect',
name: 'Software Architect',
- description:
- 'Handle software architecture planning, directory structure design, and project organization tasks',
- promptPath: path.join(__dirname, '..', 'prompts', 'software-architect.md'),
+ description: 'Handle software architecture planning, directory structure design, and project organization tasks',
+ },
+ {
+ id: 'system-analyst',
+ name: 'System Analyst',
+ description: 'Handle system analysis and requirements gathering tasks',
+ },
+
+ // dev-codemachine sub-agents
+ {
+ id: 'founder-architect',
+ name: 'Founder Architect',
+ description: 'Handle foundational architecture tasks',
+ mirrorPath: 'prompts/templates/dev-codemachine/sub-agents/architecture/01-founder-architect.md',
+ },
+ {
+ id: 'structural-data-architect',
+ name: 'Structural & Data Architect',
+ description: 'Define the static structure of the system, components hierarchy, and data organization',
+ mirrorPath: 'prompts/templates/dev-codemachine/sub-agents/architecture/02-structural-data-architect.md',
},
+ {
+ id: 'behavior-architect',
+ name: 'Behavior & Communication Architect',
+ description: 'Define dynamic interactions, data flows, and communication patterns between components',
+ mirrorPath: 'prompts/templates/dev-codemachine/sub-agents/architecture/03-behavior-architect.md',
+ },
+ {
+ id: 'ui-ux-architect',
+ name: 'UI/UX & Interface Architect',
+ description: 'Define user interface architecture, design systems, component hierarchies, and user experience patterns',
+ mirrorPath: 'prompts/templates/dev-codemachine/sub-agents/architecture/05-ui-ux-architect.md',
+ },
+ {
+ id: 'operational-architect',
+ name: 'Operational & Documentation Architect',
+ description: 'Handle deployment, operations, security, and documentation architecture',
+ mirrorPath: 'prompts/templates/dev-codemachine/sub-agents/architecture/04-operational-architect.md',
+ },
+ {
+ id: 'file-assembler',
+ name: 'File Assembler',
+ description: 'Execute commands and create manifest files from architecture outputs',
+ mirrorPath: 'prompts/templates/dev-codemachine/sub-agents/architecture/06-file-assembler.md',
+ }
];
diff --git a/docs/architecture.md b/docs/architecture.md
new file mode 100644
index 00000000..8537d05a
--- /dev/null
+++ b/docs/architecture.md
@@ -0,0 +1,316 @@
+# Overview
+
+**CodeMachine is an orchestration platform that lets you achieve any complex coding objective through customizable agent workflows. Whether you need to refactor a legacy system, migrate between frameworks, generate documentation, or create entirely new applications, the platform provides the infrastructure to coordinate specialized AI agents for any coding task.**
+
+**CodeMachine's default workflow template enables you to transform specifications directly into production-ready codebases, providing immediate value out of the box. Beyond this foundation, the platform's extensible architecture empowers you to craft custom workflows tailored to your unique development pipeline and requirements.**
+
+## Who It's For
+
+CodeMachine is built for developers, tech leads, and engineering teams who want to accelerate development without sacrificing code quality or architectural consistency.
+
+## What You Can Achieve
+
+### For Individual Developers
+
+- **Full applications generated** - Complete codebases ready for production
+- **Zero boilerplate writing** - Focus on features, not setup
+
+### For Engineering Teams
+
+- **Orchestrate any workflow** - From simple tasks to complex migrations
+- **Shared agent context** - File-based or memory context preservation
+- **Grow without limits** - Same tool for 500 or 10,000 file projects
+
+---
+
+# Quick Start
+
+## Prerequisites
+Before starting, ensure your environment meets these requirements:
+
+| Requirement | Minimum Version | Notes |
+|------------|----------------|-------|
+| Node.js | 20.10.0 | Required for running CodeMachine CLI |
+| npm | 9.0.0 | Package manager (pnpm also supported) |
+| AI Engine CLI | Latest | At least one: Codex CLI, Claude Code CLI, Cursor CLI, CCR CLI, or OpenCode CLI |
+
+## Get your first project generated
+
+```bash
+# Install CodeMachine
+npm install -g codemachine
+```
+
+```bash
+# Run CodeMachine inside your project folder
+cd my-awesome-project
+codemachine
+```
+
+Write a sample specifications:
+```
+Create a small, single-user to-do application that MUST support create, read, update, and delete operations.
+```
+
+```bash
+# Run the workflow inside CodeMachine shell
+/start
+```
+
+**Note:** You can use `--spec ` to specify a custom specification file path (Default: `.codemachine/inputs/specifications.md`)
+
+# How CodeMachine Works
+
+CodeMachine's core innovation is breaking down complex coding workflows into small, manageable tasks that AI agents can effectively handle. Instead of overwhelming a single agent with an entire project specification—which often fails due to context limitations and complexity—the platform uses workflow templates to decompose work into discrete, achievable steps.
+
+The workflow template establishes a sequence of steps, with each step representing a main agent. These main agents can invoke sub-agents through prompt-driven commands to complete various tasks. Main agents have full capability to write code and orchestrate complex operations, and can utilize sub-agents as needed - though both main and sub-agents are capable of handling any type coding of task.
+
+
+
+
+
+# Agents in CodeMachine
+
+## Overview
+
+CodeMachine's workflow system is built around three fundamental components: **Main Agents**, **Sub Agents**, and **Modules**. These components work together to execute complex, multi-step workflows with specialized capabilities and intelligent orchestration.
+
+---
+
+## Main Agents
+
+Main agents serve as the primary execution steps in a workflow. Each main agent represents a discrete phase or task in your development process.
+
+### Key Characteristics
+- **Workflow Steps**: A main agent is a step in the workflow execution
+- **Orchestration Capability**: Can delegate tasks to and coordinate sub agents
+- **Module Integration**: Can leverage specialized modules for advanced workflow behaviors
+
+---
+
+## Sub Agents
+
+Sub agents are specialized agents that operate under the coordination of main agents. They provide focused expertise and can be dynamically invoked based on workflow requirements.
+
+### Key Characteristics
+- **Orchestrated Execution**: Called and managed by main agents
+- **Specialized Focus**: Designed for specific tasks or domains
+- **Same Structure**: Share the same core properties as main agents
+
+---
+
+## Modules
+
+Modules are specialized workflow components that are implemented as agents but trigger specific execution behaviors.
+
+### What Modules Enable
+- **Conditional Logic**: Trigger specific agents based on runtime conditions
+- **Loop Constructs**: Implement iterative workflows (e.g., back loop steps)
+- **Dynamic Routing**: Direct execution flow based on intermediate results
+
+---
+
+## Agent Properties
+
+Every agent (both main and sub) is defined by four core properties:
+
+### 1. Identity
+- **Unique ID**: A distinct identifier for the agent
+- **Descriptive Name**: A human-readable name that conveys the agent's role
+
+### 2. Purpose
+A focused description that clearly defines the agent's role and responsibilities within the workflow.
+
+### 3. Prompt
+Path to a prompt template file that defines the agent's behavior, instructions, and operational context. This template guides the AI's actions when executing as this agent.
+
+### 4. AI Profile
+Configuration settings that control the AI engine:
+- **model**: Specifies which AI model to use for this agent
+- **modelReasoningEffort**: Controls the reasoning depth and execution approach
+
+---
+
+## Dynamic Agent Generation
+
+Main agents are defined using carefully engineered prompts with built-in guard rails to ensure consistent and reliable performance. In contrast, sub-agent prompts can be optionally generated by the main agents themselves. For example, an agent builder main agent can create specialized sub-agents on-demand, crafting their prompts with project-specific backgrounds and instructions tailored to the exact requirements of the current task. This dynamic prompt generation capability allows the system to adapt and create perfectly suited sub-agents for each unique project context.
+
+
+
+
+
+---
+
+# Agent Communication Patterns
+
+## 1. Sequential Hierarchical Communication
+**Pattern:** Main Agents Sequential Execution
+
+### Overview
+This pattern enables a linear workflow where multiple main agents execute in a predetermined sequence, with each agent completing its task before triggering the next agent in the chain.
+
+
+
+
+
+### What You Can Achieve
+- **Phased Development Workflows**: Perfect for projects requiring distinct phases (e.g., design → implementation → testing)
+- **Dependency Management**: Ensures tasks with strict dependencies are executed in the correct order
+- **Progressive Refinement**: Each agent can build upon the output of the previous one, refining and enhancing the codebase
+- **Checkpoint Validation**: Natural breakpoints between agents allow for validation and quality checks
+
+---
+
+## 2. Parent-Child Agent-to-Agent Communication
+**Pattern:** Main Agent with SubAgent Delegation
+
+### Overview
+The main agent acts as an orchestrator, delegating specific tasks to specialized SubAgents while maintaining overall control and context management.
+
+### Implementation Examples
+
+#### a. **Basic Parent-Child Call**
+- Main agent identifies a specialized task (e.g., frontend component generation)
+- Delegates to appropriate SubAgent with specific context
+- Receives results and integrates them into the broader workflow
+- **Achievement**: Modular task execution with specialized expertise
+
+
+
+
+
+#### b. **Multi-SubAgent Coordination** (Parallel or Sequential)
+- Main agent analyzes project requirements
+- Spawns multiple SubAgents for different components (Frontend, Backend, DevOps)
+- Can execute SubAgents in parallel for independent tasks or sequentially for dependent ones
+- Main agent acts as evaluator, ensuring consistency across all outputs
+- **Achievement**: Rapid development through parallel execution while maintaining architectural coherence
+
+
+
+
+
+#### c. **Orchestrated Evaluation Workflow** (Context Manager Pattern)
+- Main agent initiates parallel execution (green-colored agents in diagram)
+- Multiple SubAgents work simultaneously on their domains
+- Upon completion, a specialized evaluator agent (yellow-colored) is called sequentially
+- Evaluator reviews all parallel outputs for quality, consistency, and integration readiness
+- **Achievement**: Quality assurance through peer review while maximizing throughput
+
+
+
+
+
+### What You Can Achieve
+- **Dynamic Task Distribution**: Automatically scale agent deployment based on project complexity
+- **Specialized Expertise**: Each SubAgent can be optimized for specific technologies or frameworks
+- **Bidirectional Communication**: SubAgents can request clarification or additional context from the parent
+- **Adaptive Workflows**: Runtime adjustment of agent configuration based on intermediate results
+- **Comprehensive Testing**: QA SubAgent can validate work from all other agents before final delivery
+- **Resource Optimization**: Parallel execution for independent tasks, sequential for dependent operations
+
+### Advanced Capabilities
+- **Context Preservation**: Main agent maintains global project context while SubAgents focus on local optimization
+- **Error Recovery**: If a SubAgent fails, the main agent can retry with modified parameters or delegate to an alternative agent
+- **Progressive Enhancement**: Start with basic implementation, then layer additional SubAgents for optimization, security, and performance
+- **Cross-Domain Integration**: Coordinate between different technology stacks through specialized SubAgents
+
+# Agent Context Management Types
+
+## 1. File-Based Main Agent Memory
+
+### Overview
+File-based memory enables persistent context sharing between agents through structured file systems, allowing complex workflows to maintain state and share information across multiple agent executions.
+
+### a. Standard Workflow Memory (JSON-Based)
+
+#### How It Works
+- After each agent completes its task, a structured memory file (JSON) is automatically created
+- Contains execution metadata, outputs, decisions, and key context points
+- Subsequent agents can query this memory repository to retrieve relevant context
+- Memory files are indexed and searchable for efficient context retrieval
+
+#### What You Can Achieve
+- **Historical Context Preservation**: Maintain complete execution history across agent runs
+- **Intelligent Context Retrieval**: Agents can search previous executions for similar patterns or solutions
+- **Prompt-Driven Context Loading**: Dynamically load specific context based on current task requirements
+- **Debugging & Auditing**: Complete trace of all agent decisions and outputs for troubleshooting
+
+### b. Markdown File Pipeline (Sequential Context Transfer)
+
+#### How It Works
+- Main Agent A generates comprehensive markdown documentation (A.md)
+- Main Agent B reads A.md directly through prompt placeholders
+- Agent B processes and enhances, creating B.md with accumulated knowledge
+- Chain continues with each agent building upon previous documentation
+
+#### What You Can Achieve
+- **Progressive Documentation**: Each agent adds layers of detail and refinement
+- **Structured Knowledge Transfer**: Markdown format ensures human-readable context
+- **Template-Based Generation**: Use placeholders in prompts for dynamic content injection
+- **Version Control Friendly**: Markdown files integrate seamlessly with Git workflows
+
+
+
+
+
+---
+
+## 2. Orchestrator Agent Session Memory
+
+### Overview
+In parent-child communication patterns, the orchestrator (main agent) maintains centralized session memory, enabling sophisticated coordination and context management across multiple SubAgents.
+
+### How It Works
+- Main Agent initiates SubAgent with specific task and context
+- SubAgent processes task within its specialized domain
+- **Session memory returns to Main Agent** with:
+ - Task results
+ - Learned patterns
+ - Decisions made
+ - Potential issues or recommendations
+- Main Agent integrates session memory into global context
+- Continues execution with enriched understanding
+
+
+
+
+
+### What You Can Achieve
+
+#### **Centralized Intelligence**
+- Main agent becomes progressively smarter as it accumulates SubAgent insights
+- Cross-domain learning: Frontend discoveries can inform Backend decisions
+- Pattern recognition across multiple SubAgent executions
+
+#### **Dynamic Adaptation**
+- Adjust subsequent SubAgent parameters based on accumulated session memory
+- Redistribute tasks if certain approaches prove unsuccessful
+- Real-time workflow optimization based on intermediate results
+
+#### **Context Propagation**
+- Selective context sharing: Only relevant portions sent to each SubAgent
+- Prevents context pollution while maintaining necessary information
+- Efficient memory usage through intelligent filtering
+
+#### **Quality Assurance**
+- Main agent can validate SubAgent outputs against accumulated context
+- Detect inconsistencies or conflicts between different SubAgent results
+- Ensure architectural coherence across all components
+
+### Advanced Capabilities
+
+#### **Bidirectional Learning**
+- SubAgents can query main agent for clarification
+- Main agent updates global strategy based on SubAgent feedback
+- Continuous refinement loop throughout execution
+
+#### **Parallel Session Merging**
+- When multiple SubAgents run in parallel, session memories merge intelligently
+- Conflict resolution strategies for contradictory findings
+- Consensus building across multiple agent perspectives
+
+#### **Checkpoint & Recovery**
+- Session memory enables resumption from any point
+- Failed SubAgent tasks can be retried with adjusted parameters
+- Partial results preserved even in failure scenarios
diff --git a/docs/assets/demo.gif b/docs/assets/demo.gif
new file mode 100644
index 00000000..475ee82e
Binary files /dev/null and b/docs/assets/demo.gif differ
diff --git a/assets/images/codemachine-demo.gif b/docs/assets/demo.legacy.gif
similarity index 100%
rename from assets/images/codemachine-demo.gif
rename to docs/assets/demo.legacy.gif
diff --git a/assets/images/CodeMachine.png b/docs/assets/logo.png
similarity index 100%
rename from assets/images/CodeMachine.png
rename to docs/assets/logo.png
diff --git a/docs/case-studies/sustaina.md b/docs/case-studies/sustaina.md
new file mode 100644
index 00000000..92eec1e9
--- /dev/null
+++ b/docs/case-studies/sustaina.md
@@ -0,0 +1,1583 @@
+# Case Study: Sustaina ESG Compliance Platform
+## AI-Orchestrated Development of Enterprise-Grade Sustainability Compliance System via CodeMachine
+
+**Document Version:** 1.0
+**Publication Date:** October 13, 2025
+**Project Duration:** 10 weeks
+**Technology Stack:** React, Python/FastAPI, Node.js/NestJS, PostgreSQL, MongoDB, Redis, AWS, Kubernetes
+**Generated by:** CodeMachine AI Orchestration Platform
+
+---
+
+## Executive Summary
+
+Sustaina is an AI-enabled Environmental, Social, and Governance (ESG) compliance platform designed to democratize sustainability reporting for Small and Medium Enterprises (SMEs) in Egypt and the MENA region. The platform transforms complex international regulations—including the EU's Carbon Border Adjustment Mechanism (CBAM), European Sustainability Reporting Standards (ESRS), ISO 14064, and GHG Protocol—into clear, actionable compliance pathways.
+
+This case study documents how **CodeMachine**, a CLI-native AI orchestration platform, transformed a 187-page specification into a production-ready, enterprise-grade system comprising:
+
+- **7 microservices** (Python/FastAPI, Node.js/NestJS)
+- **Multi-database architecture** (PostgreSQL, MongoDB, Redis, Elasticsearch)
+- **Event-driven workflows** (Amazon SQS/SNS)
+- **Cloud-native infrastructure** (AWS EKS, RDS, ElastiCache, S3)
+- **Complete CI/CD pipeline** (GitHub Actions, ArgoCD, Terraform)
+- **Comprehensive monitoring** (Prometheus, Grafana, ELK Stack)
+
+**Key Achievement:** CodeMachine coordinated specialized AI agents across a multi-phase orchestration workflow to deliver 482 production-ready files (60,008 lines of code), complete infrastructure-as-code, and automated deployment pipelines—all generated from specification documents through intelligent agent orchestration.
+
+---
+
+## Table of Contents
+
+1. [Project Overview](#1-project-overview)
+2. [Technical Challenge & Requirements](#2-technical-challenge--requirements)
+3. [CodeMachine Orchestration Platform](#3-codemachine-orchestration-platform)
+4. [Architecture & Technology Stack](#4-architecture--technology-stack)
+5. [Implementation Strategy](#5-implementation-strategy)
+6. [Complete Project Structure](#6-complete-project-structure)
+7. [Key Implementation Patterns](#7-key-implementation-patterns)
+8. [Results & Deliverables](#8-results--deliverables)
+9. [Technical Metrics](#9-technical-metrics)
+
+---
+
+## 1. Project Overview
+
+### 1.1 Business Context
+
+SMEs in the MENA region face mounting pressure to demonstrate ESG compliance to access European markets and international financing. The EU's CBAM regulation, effective 2026, requires exporters to report embedded carbon emissions in products. ESRS mandates detailed sustainability disclosures for companies operating in EU markets. For resource-constrained SMEs, navigating these complex, multi-jurisdictional frameworks represents a significant barrier to growth.
+
+**Sustaina's Mission:** Provide an intelligent compliance assistant that:
+- **Automates regulatory intelligence**: Maps jurisdiction-specific requirements based on industry, location, and target markets
+- **Simplifies carbon accounting**: Calculates product-level Scope 1-3 emissions using verified emission factors
+- **Ensures audit readiness**: AI-powered document verification against regulatory requirements
+- **Enables market access**: Generates CBAM-compliant reports and ESG disclosures
+
+### 1.2 Project Scope
+
+**Phase 1 (Current):** Carbon Accounting & CBAM Compliance
+- Jurisdiction-aware compliance mapping (EU, UK, KSA, Egypt)
+- Product-level embedded emissions calculation
+- AI-powered document processing (invoices, EPDs, energy bills)
+- Supply chain emission mapping
+- CBAM report generation
+- Compliance risk dashboards
+
+**Phase 2 (Planned):** Full ESG Reporting
+- Materiality-based disclosure filtering (74 ESRS topics → 8-12 material issues)
+- Social metrics tracking (labor, diversity, human rights)
+- Governance metrics (ethics, data protection, board composition)
+- Multi-framework report generation (ESRS, GRI, IFC Performance Standards)
+
+**Out of Scope:**
+- Carbon credit trading mechanisms
+- Blockchain-based supply chain traceability (future consideration)
+- Physical product verification or IoT integration
+- Direct regulatory submission (system generates compliant reports for manual submission)
+
+---
+
+## 2. Technical Challenge & Requirements
+
+### 2.1 Functional Requirements
+
+| Requirement ID | Description | Technical Impact |
+|---------------|-------------|------------------|
+| **FR-COMP-001** | Multi-jurisdiction framework mapping (EU CBAM, ESRS, ISO 14064, KSA PDPL, Egypt regulations) | Requires flexible regulatory rule engine with versioned framework definitions in MongoDB |
+| **FR-CARBON-001** | Product-level Scope 1-3 emissions calculation using verified emission factors | Demands integration with licensed databases (Ecoinvent, DEFRA, EPA), complex aggregation logic |
+| **FR-AI-001** | Multi-format document ingestion (PDF, images, Excel) with LLM-based field extraction | Requires OCR pipeline (PyTesseract) + GPT-4 API integration with prompt engineering |
+| **FR-CBAM-002** | Export-ready CBAM reports (kg CO₂e per unit) for EU importers | Necessitates PDF generation with standardized templates, S3 storage, audit trails |
+| **FR-RISK-001** | Traffic-light compliance risk scoring (Green/Yellow/Orange/Red) | Real-time risk calculation engine with evidence gap detection and remediation logic |
+| **FR-SC-001** | Supply chain emission mapping with substitution logic for missing data | Graph-based supplier relationships, intelligent default emission factor selection |
+
+### 2.2 Non-Functional Requirements
+
+| Category | Requirement | Architectural Solution |
+|----------|-------------|----------------------|
+| **Performance** | <60s processing for compliance calculations (≤100 suppliers) | Asynchronous event-driven workflows (SQS), Redis caching, optimized database queries |
+| **Accuracy** | >90% AI extraction accuracy (structured docs), >80% (semi-structured) | Multi-stage validation pipeline, confidence scoring, human-in-the-loop for low-confidence extractions |
+| **Reliability** | 99.5% uptime (≈3.6 hours downtime/month) | Multi-AZ deployment, health checks, circuit breakers, automated failover (RDS Multi-AZ) |
+| **Security** | GDPR and KSA PDPL compliance | AES-256 encryption at rest, TLS 1.3 in transit, RBAC, audit logging, regional data residency |
+| **Scalability** | 1,000+ concurrent users | Horizontal pod auto-scaling (Kubernetes HPA), stateless services, distributed caching |
+| **Extensibility** | Easy integration of new ESG frameworks | Rule engine with JSON-based framework definitions, versioned APIs, adapter pattern |
+
+### 2.3 Technical Constraints
+
+1. **Multi-Tenancy:** Strict data isolation between SME tenants (row-level security with `company_id` partition key)
+2. **Regional Compliance:** Data residency requirements necessitate multi-region deployment (EU-Central-1 for GDPR, ME-South-1 for KSA PDPL)
+3. **Document Variability:** Must handle diverse document formats with varying quality (handwritten invoices, scanned PDFs, digital exports)
+4. **Emission Data Licensing:** Dependency on third-party databases with annual subscription costs and access limits
+5. **Regulatory Volatility:** CBAM and ESRS specifications evolving, requiring version-controlled framework updates
+
+---
+
+## 3. CodeMachine Orchestration Platform
+
+### 3.1 Platform Architecture
+
+**CodeMachine** is a CLI-native orchestration platform that transforms specification files and contextual inputs into production-ready code through coordinated multi-agent workflows. Unlike traditional code generation tools that produce monolithic outputs, CodeMachine employs:
+
+1. **Hierarchical Agent Orchestration:** Specialized AI agents operate in parent-child relationships with bidirectional communication
+2. **Runtime-Adaptable Methodologies:** Dynamic workflow adjustment based on project requirements without framework modifications
+3. **Context-Aware Task Decomposition:** Intelligent breakdown of specifications into parallelizable, dependency-tracked tasks
+4. **Verification Loops:** Continuous validation of generated artifacts against specifications and cross-artifact consistency checks
+
+### 3.2 Core Components
+
+```
+.codemachine/
+├── inputs/
+│ └── specifications.md # Source requirements (187 pages)
+├── artifacts/
+│ ├── architecture/ # Generated architecture blueprints
+│ │ ├── 01_Context_and_Drivers.md
+│ │ ├── 02_Architecture_Overview.md
+│ │ ├── 03_System_Structure_and_Data.md
+│ │ ├── 04_Behavior_and_Communication.md
+│ │ ├── 05_Operational_Architecture.md
+│ │ ├── 06_Rationale_and_Future.md
+│ │ └── architecture_manifest.json
+│ ├── plan/ # Iteration plans and task breakdowns
+│ │ ├── 01_Plan_Overview_and_Setup.md
+│ │ ├── 02_Iteration_I{1-5}.md
+│ │ ├── 03_Verification_and_Glossary.md
+│ │ └── plan_manifest.json
+│ └── tasks/ # Granular task specifications
+│ ├── tasks_I1.json (11 tasks)
+│ ├── tasks_I2.json (7 tasks)
+│ ├── tasks_I3.json (7 tasks)
+│ ├── tasks_I4.json (7 tasks)
+│ ├── tasks_I5.json (7 tasks)
+│ └── tasks_manifest.json
+├── prompts/
+│ ├── context.md # Dynamic context injection
+│ ├── plan_fallback.md
+│ ├── task_fallback.md
+│ └── code_fallback.md
+├── agents/
+│ └── agents-config.json # Agent type registry
+└── template.json # Workflow orchestration state
+```
+
+### 3.3 Orchestration Workflow
+
+**Phase 1: Specification Analysis**
+```
+Input: specifications.md (187 pages)
+ ↓
+[Architecture Agent] → Analyzes requirements, identifies architectural drivers
+ ↓
+Output: 6 architecture blueprint documents with C4 diagrams, ERDs, sequence diagrams
+```
+
+**Phase 2: Strategic Planning**
+```
+Inputs: Architecture blueprints + specifications
+ ↓
+[Planning Agent] → Decomposes into 5 iterations with 39 tasks
+ ↓
+Outputs:
+ - Iteration plans with goals and acceptance criteria
+ - Task dependency graphs
+ - Parallelization strategy
+ - Verification checkpoints
+```
+
+**Phase 3: Task Decomposition**
+```
+Inputs: Iteration plans + architecture context
+ ↓
+[Task Breakdown Agent] → Creates granular task specifications
+ ↓
+Outputs:
+ - tasks_I{1-5}.json (39 total tasks)
+ - Each task includes:
+ * Detailed description
+ * Target files
+ * Input files (dependencies)
+ * Acceptance criteria
+ * Agent type hint (SetupAgent, DatabaseAgent, BackendAgent, etc.)
+ * Parallelization flag
+```
+
+**Phase 4: Code Generation & Verification**
+```
+Orchestration Workflow Steps:
+ 1. [git-commit] → Commit initial project specification (Cursor, execute once)
+ 2. [arch-agent] → Define system architecture and technical design (Claude, execute once)
+ 3. [plan-agent] → Generate comprehensive iterative development plan (Claude, execute once)
+ 4. [task-breakdown] → Extract and structure tasks into JSON (Claude, execute once)
+ 5. [git-commit] → Commit task breakdown (Cursor, execute once)
+
+ For each task (loop: max 20 iterations):
+ 6. [context-manager] → Gather relevant context from architecture/plan/codebase (Claude)
+ 7. [code-generation] → Generate code implementation (Codex)
+ 8. [cleanup-code-fallback] → Delete .codemachine/prompts/code_fallback.md if present (Cursor)
+ 9. [runtime-prep] → Generate shell scripts (install, run, lint, test) (Codex, execute once)
+ 10. [task-sanity-check] → Verify code against requirements (Codex)
+ 11. [git-commit] → Commit generated and verified code (Cursor)
+ 12. [check-task] → Loop back if tasks incomplete (Cursor, skip runtime-prep on loops)
+```
+
+### 3.4 Orchestration Agents
+
+CodeMachine employs **specialized orchestration agents** with distinct AI engines:
+
+| Agent | Engine | Execution | Responsibilities |
+|-------|--------|-----------|------------------|
+| **git-commit** | Cursor | Once/Per-task | Commits specifications, task breakdowns, and generated code to version control |
+| **arch-agent** | Claude | Once | Analyzes requirements, defines system architecture, creates C4 diagrams, ERDs, and technical design decisions |
+| **plan-agent** | Claude | Once | Generates comprehensive iterative development plan with task decomposition, dependencies, and acceptance criteria |
+| **task-breakdown** | Claude | Once | Extracts and structures tasks from plan into JSON format with detailed specifications, file paths, and agent hints |
+| **context-manager** | Claude | Per-task | Gathers relevant context from architecture blueprints, plan documents, and existing codebase for task execution |
+| **code-generation** | Codex | Per-task | Generates implementation code including microservices, APIs, infrastructure-as-code, frontend components, and tests |
+| **cleanup-code-fallback** | Cursor | Per-task | Removes temporary fallback files from prompts directory to maintain clean workflow state |
+| **runtime-prep** | Codex | Once | Generates robust shell scripts for project automation (install.sh, run.sh, lint.sh, test.sh) |
+| **task-sanity-check** | Codex | Per-task | Verifies generated code against task requirements, acceptance criteria, and architectural constraints |
+| **check-task** | Cursor | Per-task | Evaluates task completion status and triggers loop iteration if tasks remain incomplete (max 20 iterations) |
+
+**Multi-Engine Strategy:**
+- **Claude (Sonnet 4.5):** Strategic planning, architecture design, context analysis (superior reasoning)
+- **Codex (GPT-5 medium):** Code generation, verification, runtime tooling (optimized for code synthesis)
+- **Cursor (Cheetah stealth model):** Version control operations, cleanup tasks (file system operations)
+
+### 3.5 Context Injection
+
+**Dynamic Context Provision:**
+Each agent receives task-specific context extracted from:
+- Architecture blueprints (via `architecture_manifest.json` anchor references)
+- Plan documents (via `plan_manifest.json` section references)
+- Existing codebase analysis (CodeMachine scans modified files)
+- Cross-cutting concerns (authentication, logging patterns)
+
+**Example Context for Task I2.T1 (Document Processing Service):**
+```markdown
+### Context: component-diagram (from docs/architecture/03_System_Structure_and_Data.md)
+Shows Document Processing Service internal components:
+- Upload API Controller
+- OCR Engine (PyTesseract)
+- LLM Extraction Pipeline (LangChain + GPT-4)
+- Validation Engine (Regulatory ruleset checker)
+- Evidence Repository (S3 integration)
+- Event Publisher (SQS)
+
+### Relevant Existing Code
+- File: scripts/schema.sql:45
+ Summary: Document table with columns: document_id, company_id, document_type,
+ s3_key, file_size, uploaded_at, status
+ Recommendation: Use this schema for database models; add foreign key to DocumentExtraction
+```
+
+---
+
+## 4. Architecture & Technology Stack
+
+### 4.1 Architectural Style
+
+**Event-Driven Microservices Architecture**
+
+**Rationale:**
+1. **Domain Separation:** 7 microservices aligned with business capabilities (Compliance, Carbon Accounting, Document Processing, Risk Assessment, Supply Chain, ESG Reporting, Notifications)
+2. **Asynchronous Processing:** Document analysis and emissions calculations are time-intensive (violates 60s SLA if synchronous)
+3. **Independent Scaling:** AI processing requires more compute than compliance mapping
+4. **Technology Diversity:** Python for AI/ML workloads, Node.js for high-throughput CRUD APIs
+5. **Resilience:** Service isolation prevents cascading failures (critical for 99.5% uptime target)
+
+**Event Flows:**
+- **Document Processing:** Upload → OCR → LLM Extraction → Validation → Risk Update → Notification
+- **Carbon Calculation:** Request → Supplier Fetch → Emission Factor Lookup → Calculation → Report Generation → Notification
+
+### 4.2 Technology Stack
+
+#### Frontend Layer
+- **Framework:** React 18 with TypeScript
+- **State Management:** React Query (server state), Context API (UI state)
+- **Styling:** Tailwind CSS with custom design system
+- **Build Tools:** Vite (fast HMR, optimized production builds)
+- **Internationalization:** i18next (English, Arabic)
+
+#### API Layer
+- **Gateway:** Kong Gateway (JWT validation, rate limiting, routing)
+- **Protocol:** RESTful APIs with OpenAPI 3.1 specifications
+- **Documentation:** Auto-generated Swagger UI and Redoc
+
+#### Backend Services
+**AI/ML Services (Python 3.11 + FastAPI):**
+- Document Processing Service
+- Carbon Accounting Service
+- Compliance Engine Service
+- ESG Reporting Service
+
+**CRUD APIs (Node.js 20 + NestJS):**
+- Risk Assessment Service
+- Supply Chain Service
+- Notification Service
+
+**Key Libraries:**
+- **AI:** LangChain, OpenAI SDK, PyTesseract, PyPDF2, Pillow
+- **Calculations:** NumPy, Pandas (emissions aggregation)
+- **Validation:** Pydantic (FastAPI), class-validator (NestJS)
+
+#### Data Layer
+- **Primary Database:** PostgreSQL 15 (AWS RDS Multi-AZ)
+ - ACID compliance for financial/regulatory data
+ - JSON columns for flexible framework definitions
+ - PostGIS for geospatial jurisdiction mapping
+- **Document Store:** MongoDB Atlas (AWS DocumentDB)
+ - Regulatory framework schemas (CBAM, ESRS, ISO)
+ - Flexible schema for evolving regulations
+- **Cache:** Redis 7 (AWS ElastiCache)
+ - Emission factor caching (30-day TTL)
+ - API response caching (5-minute TTL for dashboards)
+ - Session management
+- **Object Storage:** AWS S3
+ - Document storage (invoices, EPDs, certificates)
+ - Generated reports (CBAM PDFs, audit packs)
+ - AES-256 encryption, versioning for audit trails
+- **Search Engine:** Elasticsearch 8 (AWS OpenSearch)
+ - Full-text search for regulations
+ - Supplier lookup
+ - Audit log analysis
+
+#### Message & Event Layer
+- **Message Queue:** Amazon SQS (Standard + FIFO queues)
+- **Event Bus:** Amazon SNS (Pub/Sub patterns)
+- **Dead Letter Queue:** SQS DLQ for failed message handling
+
+#### Infrastructure & Deployment
+- **Containerization:** Docker (all microservices)
+- **Orchestration:** Amazon EKS (Kubernetes 1.28+)
+- **IaC:** Terraform (VPC, EKS, RDS, ElastiCache, S3, SQS modules)
+- **Service Mesh:** Istio (mTLS, circuit breakers, distributed tracing)
+- **CI/CD:** GitHub Actions (pipelines), ArgoCD (GitOps deployments)
+
+#### Monitoring & Observability
+- **Metrics:** Prometheus + Grafana dashboards
+- **Logging:** ELK Stack (Elasticsearch, Logstash, Kibana) + CloudWatch
+- **Tracing:** Jaeger (distributed tracing across microservices)
+- **Alerting:** PagerDuty (critical), Slack (warnings)
+
+#### Security & Authentication
+- **Identity Provider:** Auth0 (OAuth2/OIDC, enterprise SSO)
+- **Secrets Management:** HashiCorp Vault
+- **Security Scanning:** OWASP ZAP (DAST), Snyk (dependencies), SonarQube (SAST)
+
+### 4.3 Data Model Overview
+
+**Core Entities (15 tables in PostgreSQL):**
+
+**Multi-Tenancy:**
+- `Company`: Tenant entity (partition key `company_id`, jurisdiction, industry, target markets)
+- `User`: Role-based access (admin, compliance_officer, auditor, supply_chain_manager)
+
+**Compliance Management:**
+- `ComplianceChecklist`: Generated checklist per company (jurisdiction, frameworks, risk score)
+- `ChecklistItem`: Individual requirements (status, evidence links, due dates)
+- `ComplianceReport`: Generated reports (CBAM, ESRS, GRI) with S3 keys
+
+**Carbon Accounting:**
+- `Product`: SME products (HS codes, bill of materials, unit of measure)
+- `Supplier`: Supply chain entities (tier levels, location, sector)
+- `ProductSupplier`: Many-to-many with quantities
+- `EmissionCalculation`: Product-level footprint (Scope 1-3 breakdown, total CO₂e, quality score)
+- `EmissionFactor`: Cached factors (region, sector, activity, CO₂e per unit, source, validity)
+
+**Document Management:**
+- `Document`: Uploaded evidence (S3 keys, document type, metadata)
+- `DocumentExtraction`: AI-extracted fields (JSON), confidence scores, validation status
+
+**Audit & Risk:**
+- `AuditLog`: Immutable append-only log (user actions, timestamps, IP addresses)
+- `RiskAssessment`: Historical risk scores with traffic-light indicators
+
+**MongoDB Collections:**
+- `RegulatoryFramework`: JSON documents (CBAM, ESRS, ISO 14064, GRI)
+ - Disclosure requirements
+ - Calculation methods
+ - Evidence types
+ - Thresholds
+ - Temporal versioning (CBAM v1.0 → v1.1)
+
+### 4.4 API Design Principles
+
+**RESTful Conventions:**
+- **Resource-Oriented URLs:** `/companies/{companyId}/products`, `/documents/{documentId}/extractions`
+- **HTTP Verbs:** GET (retrieval), POST (creation), PUT (update), DELETE (removal)
+- **Status Codes:** 200 OK, 201 Created, 202 Accepted, 400 Bad Request, 401 Unauthorized, 404 Not Found, 500 Internal Server Error
+- **Error Format:** RFC 7807 Problem Details (`type`, `title`, `status`, `detail`)
+
+**Asynchronous Operations:**
+- Long-running tasks return `202 Accepted` with `Location` header
+- Clients poll status endpoint: `/api/v1/carbon/calculations/{id}` → `{status: "completed", result: {...}}`
+
+**Key Endpoints:**
+- `POST /api/v1/compliance/checklists` - Generate jurisdiction-specific checklist
+- `POST /api/v1/carbon/calculations` - Calculate product emissions (async)
+- `POST /api/v1/documents` - Upload document (returns presigned S3 URL)
+- `POST /api/v1/documents/{id}/extract` - Trigger AI extraction (async)
+- `GET /api/v1/risk/scores/{companyId}` - Compliance risk score
+
+---
+
+## 5. Implementation Strategy
+
+### 5.1 Iteration-Based Delivery
+
+**Iteration 1 (Foundation):** Project Setup & Core Artifacts (2 weeks)
+- **Goal:** Establish infrastructure, data models, diagrams, API specs
+- **Tasks:** 11 tasks (Setup, Diagrams, Database, Terraform, OpenAPI, Seed Scripts, Shared Libraries)
+- **Deliverables:**
+ - Complete monorepo structure
+ - PostgreSQL schema (15 tables) + MongoDB schemas
+ - PlantUML diagrams (Context, Container, Component, ERD, Deployment)
+ - OpenAPI 3.1 specs (5 services + consolidated)
+ - Terraform modules (VPC, EKS, RDS, ElastiCache)
+ - TypeScript API client (auto-generated)
+ - Seed scripts (emission factors, regulatory frameworks)
+ - Shared Python/Node.js libraries
+
+**Iteration 2 (Core Services):** Document Processing & Carbon Accounting (2 weeks)
+- **Goal:** Implement primary business logic for Phase 1
+- **Tasks:** 7 tasks (Document Service APIs, OCR+LLM pipeline, Validation, Carbon calculation engine, Sequence diagrams, Postman collection, Tests)
+- **Deliverables:**
+ - Document Processing Service (S3 presigned URLs, PyTesseract OCR, GPT-4 extraction, validation engine)
+ - Carbon Accounting Service (GHG Protocol calculation, CBAM report generation)
+ - Sequence diagrams (Document workflow, CBAM calculation)
+ - Postman collection (20+ API requests)
+ - Unit + integration tests (>80% coverage)
+
+**Iteration 3 (Compliance & UI):** Compliance Engine, Risk, Supply Chain, Dashboard (2 weeks)
+- **Goal:** Complete Phase 1 feature set with frontend
+- **Tasks:** 7 tasks (Compliance Engine, Risk Service, Supply Chain Service, Notification Service, React Dashboard, E2E tests, Staging deployment)
+- **Deliverables:**
+ - Compliance Engine (framework mapping, checklist generation)
+ - Risk Assessment Service (traffic-light scoring, gap analysis)
+ - Supply Chain Service (supplier mapping, emission factor substitution)
+ - Notification Service (email alerts, WebSocket notifications)
+ - Compliance Dashboard (React, risk visualization, document uploads)
+ - End-to-end integration tests
+ - Deployed to AWS staging (ArgoCD)
+
+**Iteration 4 (Production Readiness):** Integration, Security, Performance (2 weeks)
+- **Goal:** Harden system for production deployment
+- **Tasks:** 7 tasks (Kong Gateway, CloudFront deployment, Playwright E2E, Load testing, Security scanning, Production infrastructure, UAT)
+- **Deliverables:**
+ - Kong Gateway with JWT auth and rate limiting
+ - Web app on S3 + CloudFront CDN
+ - Playwright E2E tests (compliance, carbon, CBAM workflows)
+ - Load testing reports (k6: 1000 concurrent users)
+ - OWASP ZAP security scan results
+ - Blue-green deployment infrastructure
+ - UAT with 5 pilot SME customers
+
+**Iteration 5 (ESG Expansion - Planned):** Full ESG Reporting (3 weeks)
+- **Goal:** Phase 2 features - ESRS, GRI, social/governance metrics
+- **Tasks:** 7 tasks (Data model extension, ESG Reporting Service, Social metrics, Governance metrics, ESRS/GRI report generation, ESG Dashboard, Deployment)
+
+### 5.2 Verification Strategy
+
+**Artifact Validation:**
+- **PlantUML Diagrams:** Must render without syntax errors using PlantUML CLI
+- **OpenAPI Specs:** Validated with `openapi-generator-cli` and Swagger Editor
+- **SQL DDL:** Executed on PostgreSQL 15 without errors
+- **Terraform:** `terraform validate` passes, `terraform plan` generates valid execution plan
+- **TypeScript Client:** Compiles with `tsc` without errors
+- **Tests:** >80% code coverage requirement
+
+**Cross-Artifact Consistency Checks:**
+- ERD entities match SQL schema tables
+- OpenAPI schemas align with database models
+- Sequence diagrams validated against implemented API flows
+- Environment variables in Terraform outputs match service configurations
+
+---
+
+## 6. Complete Project Structure
+
+```
+sustaina-platform/
+├── .github/ # CI/CD Workflows
+│ └── workflows/
+│ ├── ci-backend.yml # Backend tests, linting, Docker builds
+│ ├── ci-frontend.yml # Frontend tests, build
+│ ├── cd-staging.yml # Deploy to staging
+│ ├── cd-production.yml # Deploy to production (manual)
+│ └── cd-web-app.yml # Web app S3 + CloudFront deployment
+│
+├── services/ # Microservices (7 services)
+│ ├── compliance-service/ # Python/FastAPI
+│ │ ├── src/
+│ │ │ ├── api/ # Route handlers
+│ │ │ │ ├── __init__.py
+│ │ │ │ ├── checklists.py # POST/GET /checklists
+│ │ │ │ ├── frameworks.py # GET /frameworks
+│ │ │ │ └── health.py
+│ │ │ ├── core/ # Business logic
+│ │ │ │ ├── checklist_generator.py # Jurisdiction-aware checklist logic
+│ │ │ │ ├── framework_loader.py # MongoDB framework queries
+│ │ │ │ └── rule_engine.py # Regulatory rule evaluation
+│ │ │ ├── models/ # SQLAlchemy ORM
+│ │ │ │ ├── company.py
+│ │ │ │ ├── checklist.py
+│ │ │ │ └── checklist_item.py
+│ │ │ ├── schemas/ # Pydantic validation
+│ │ │ │ ├── checklist_request.py
+│ │ │ │ └── checklist_response.py
+│ │ │ └── main.py # FastAPI app entry point
+│ │ ├── tests/ # Pytest
+│ │ │ ├── test_checklist_generator.py
+│ │ │ └── test_frameworks.py
+│ │ ├── Dockerfile # Multi-stage build
+│ │ ├── requirements.txt
+│ │ └── README.md
+│ │
+│ ├── carbon-accounting-service/ # Python/FastAPI
+│ │ ├── src/
+│ │ │ ├── api/
+│ │ │ │ ├── calculations.py # POST /calculations, GET /calculations/{id}
+│ │ │ │ ├── reports.py # POST /reports/cbam
+│ │ │ │ └── emission_factors.py
+│ │ │ ├── core/
+│ │ │ │ ├── ghg_calculator.py # Scope 1-3 calculation engine
+│ │ │ │ ├── cbam_generator.py # CBAM PDF report generation
+│ │ │ │ └── emission_factor_service.py
+│ │ │ ├── models/
+│ │ │ │ ├── product.py
+│ │ │ │ ├── emission_calculation.py
+│ │ │ │ └── emission_factor.py
+│ │ │ ├── schemas/
+│ │ │ ├── workers/ # Async workers
+│ │ │ │ └── calculation_worker.py # SQS consumer
+│ │ │ └── main.py
+│ │ ├── tests/
+│ │ │ ├── test_ghg_calculator.py # Verify calculation accuracy (<5% variance)
+│ │ │ └── test_cbam_generator.py
+│ │ ├── Dockerfile
+│ │ ├── requirements.txt
+│ │ └── README.md
+│ │
+│ ├── document-processing-service/ # Python/FastAPI
+│ │ ├── src/
+│ │ │ ├── api/
+│ │ │ │ ├── documents.py # POST /documents, POST /documents/{id}/extract
+│ │ │ │ └── validations.py # GET /documents/{id}/validation
+│ │ │ ├── core/
+│ │ │ │ ├── ocr.py # PyTesseract + PyPDF2 integration
+│ │ │ │ ├── llm_pipeline.py # LangChain + GPT-4 extraction
+│ │ │ │ ├── validator.py # Regulatory ruleset validation
+│ │ │ │ └── s3_client.py # S3 presigned URL generation
+│ │ │ ├── models/
+│ │ │ │ ├── document.py
+│ │ │ │ └── document_extraction.py
+│ │ │ ├── schemas/
+│ │ │ ├── workers/
+│ │ │ │ ├── ocr_worker.py # SQS consumer for OCR
+│ │ │ │ ├── extraction_worker.py # SQS consumer for LLM
+│ │ │ │ └── validation_worker.py
+│ │ │ └── main.py
+│ │ ├── tests/
+│ │ │ ├── test_ocr.py
+│ │ │ ├── test_llm_pipeline.py # Mock GPT-4 calls
+│ │ │ └── test_validator.py
+│ │ ├── Dockerfile
+│ │ ├── requirements.txt
+│ │ └── README.md
+│ │
+│ ├── risk-assessment-service/ # Node.js/NestJS
+│ │ ├── src/
+│ │ │ ├── controllers/
+│ │ │ │ ├── risk.controller.ts # GET /scores/{companyId}, GET /gaps
+│ │ │ │ └── health.controller.ts
+│ │ │ ├── services/
+│ │ │ │ ├── risk-calculator.service.ts # Traffic-light scoring algorithm
+│ │ │ │ ├── gap-analyzer.service.ts # Evidence gap detection
+│ │ │ │ └── recommendation.service.ts
+│ │ │ ├── entities/
+│ │ │ │ └── risk-assessment.entity.ts # TypeORM entity
+│ │ │ ├── dto/
+│ │ │ │ ├── risk-score.dto.ts
+│ │ │ │ └── gap-analysis.dto.ts
+│ │ │ ├── consumers/
+│ │ │ │ └── document-validated.consumer.ts # SQS consumer
+│ │ │ └── main.ts # NestJS bootstrap
+│ │ ├── test/
+│ │ │ ├── risk-calculator.service.spec.ts
+│ │ │ └── gap-analyzer.service.spec.ts
+│ │ ├── Dockerfile
+│ │ ├── package.json
+│ │ ├── tsconfig.json
+│ │ └── README.md
+│ │
+│ ├── supply-chain-service/ # Node.js/NestJS
+│ │ ├── src/
+│ │ │ ├── controllers/
+│ │ │ │ ├── suppliers.controller.ts # CRUD /suppliers
+│ │ │ │ └── product-suppliers.controller.ts
+│ │ │ ├── services/
+│ │ │ │ ├── supplier.service.ts
+│ │ │ │ ├── product-supplier.service.ts
+│ │ │ │ └── emission-factor.service.ts # Default factor substitution
+│ │ │ ├── entities/
+│ │ │ │ ├── supplier.entity.ts
+│ │ │ │ └── product-supplier.entity.ts
+│ │ │ ├── dto/
+│ │ │ └── main.ts
+│ │ ├── test/
+│ │ ├── Dockerfile
+│ │ ├── package.json
+│ │ └── README.md
+│ │
+│ ├── esg-reporting-service/ # Python/FastAPI (Phase 2)
+│ │ ├── src/
+│ │ │ ├── api/
+│ │ │ │ ├── materiality.py
+│ │ │ │ ├── esg_metrics.py
+│ │ │ │ └── reports.py # POST /reports/esrs, POST /reports/gri
+│ │ │ ├── core/
+│ │ │ │ ├── materiality_assessor.py # Filter 74 ESRS topics → 8-12 material
+│ │ │ │ ├── esg_calculator.py
+│ │ │ │ └── report_generator.py
+│ │ │ ├── models/
+│ │ │ ├── schemas/
+│ │ │ └── main.py
+│ │ ├── tests/
+│ │ ├── Dockerfile
+│ │ ├── requirements.txt
+│ │ └── README.md
+│ │
+│ └── notification-service/ # Node.js/NestJS
+│ ├── src/
+│ │ ├── controllers/
+│ │ │ └── notifications.controller.ts
+│ │ ├── services/
+│ │ │ ├── email.service.ts # SendGrid/SES integration
+│ │ │ ├── websocket.service.ts # Socket.io for real-time notifications
+│ │ │ └── template.service.ts
+│ │ ├── consumers/
+│ │ │ ├── compliance-risk-changed.consumer.ts
+│ │ │ ├── calculation-completed.consumer.ts
+│ │ │ └── document-validated.consumer.ts
+│ │ ├── templates/ # Email templates (Handlebars)
+│ │ │ ├── compliance-alert.hbs
+│ │ │ └── calculation-complete.hbs
+│ │ └── main.ts
+│ ├── test/
+│ ├── Dockerfile
+│ ├── package.json
+│ └── README.md
+│
+├── web-app/ # React Frontend (SPA)
+│ ├── public/
+│ │ ├── favicon.ico
+│ │ └── index.html
+│ ├── src/
+│ │ ├── components/ # Reusable UI components
+│ │ │ ├── Button.tsx
+│ │ │ ├── Card.tsx
+│ │ │ ├── Modal.tsx
+│ │ │ ├── Table.tsx
+│ │ │ └── Spinner.tsx
+│ │ ├── features/ # Feature modules
+│ │ │ ├── compliance/
+│ │ │ │ ├── ComplianceDashboard.tsx
+│ │ │ │ ├── ChecklistView.tsx
+│ │ │ │ └── FrameworkSelector.tsx
+│ │ │ ├── carbon/
+│ │ │ │ ├── ProductList.tsx
+│ │ │ │ ├── CalculationForm.tsx
+│ │ │ │ └── CBAMReportView.tsx
+│ │ │ ├── documents/
+│ │ │ │ ├── DocumentUpload.tsx
+│ │ │ │ ├── DocumentList.tsx
+│ │ │ │ └── ValidationStatus.tsx
+│ │ │ ├── risk/
+│ │ │ │ ├── RiskDashboard.tsx
+│ │ │ │ ├── TrafficLightIndicator.tsx
+│ │ │ │ └── GapAnalysis.tsx
+│ │ │ └── auth/
+│ │ │ ├── LoginPage.tsx
+│ │ │ └── Profile.tsx
+│ │ ├── api/ # API client
+│ │ │ ├── client.ts # Axios config + generated client wrapper
+│ │ │ └── hooks/ # React Query hooks
+│ │ │ ├── useChecklists.ts
+│ │ │ ├── useCalculations.ts
+│ │ │ └── useDocuments.ts
+│ │ ├── utils/
+│ │ │ ├── formatters.ts # Date, number, currency formatting
+│ │ │ └── validators.ts
+│ │ ├── hooks/
+│ │ │ ├── useAuth.ts
+│ │ │ └── useToast.ts
+│ │ ├── i18n/ # Internationalization
+│ │ │ ├── en.json # English translations
+│ │ │ └── ar.json # Arabic translations
+│ │ ├── App.tsx
+│ │ ├── main.tsx
+│ │ └── vite-env.d.ts
+│ ├── tests/ # Jest + React Testing Library
+│ │ ├── ComplianceDashboard.test.tsx
+│ │ └── DocumentUpload.test.tsx
+│ ├── .env.staging # Environment variables
+│ ├── .env.production
+│ ├── package.json
+│ ├── vite.config.ts
+│ ├── tailwind.config.js
+│ ├── tsconfig.json
+│ └── README.md
+│
+├── infrastructure/ # Infrastructure as Code
+│ ├── terraform/
+│ │ ├── environments/
+│ │ │ ├── dev/
+│ │ │ │ ├── main.tf # Wires modules for dev environment
+│ │ │ │ ├── terraform.tfvars # Dev-specific variables
+│ │ │ │ └── backend.tf # S3 backend for state
+│ │ │ ├── staging/
+│ │ │ │ ├── main.tf
+│ │ │ │ ├── web-app.tf # S3 + CloudFront for web app
+│ │ │ │ └── terraform.tfvars
+│ │ │ └── production/
+│ │ │ ├── main.tf
+│ │ │ └── terraform.tfvars
+│ │ ├── modules/
+│ │ │ ├── vpc/
+│ │ │ │ ├── main.tf # VPC, subnets, NAT, IGW, route tables
+│ │ │ │ ├── variables.tf
+│ │ │ │ └── outputs.tf
+│ │ │ ├── eks/
+│ │ │ │ ├── main.tf # EKS cluster, node groups, RBAC
+│ │ │ │ ├── variables.tf
+│ │ │ │ └── outputs.tf
+│ │ │ ├── rds/
+│ │ │ │ ├── main.tf # PostgreSQL Multi-AZ, backups, encryption
+│ │ │ │ ├── variables.tf
+│ │ │ │ └── outputs.tf
+│ │ │ ├── elasticache/
+│ │ │ │ ├── main.tf # Redis cluster, cross-AZ replication
+│ │ │ │ ├── variables.tf
+│ │ │ │ └── outputs.tf
+│ │ │ ├── s3/
+│ │ │ │ ├── main.tf # S3 buckets (documents, backups, logs)
+│ │ │ │ ├── variables.tf
+│ │ │ │ └── outputs.tf
+│ │ │ ├── s3-web-hosting/
+│ │ │ │ ├── main.tf # S3 static site + CloudFront + ACM + Route53
+│ │ │ │ ├── variables.tf
+│ │ │ │ └── outputs.tf
+│ │ │ ├── sqs/
+│ │ │ │ ├── main.tf # SQS queues, SNS topics, DLQs
+│ │ │ │ ├── variables.tf
+│ │ │ │ └── outputs.tf
+│ │ │ └── monitoring/
+│ │ │ ├── main.tf # CloudWatch dashboards, alarms
+│ │ │ ├── variables.tf
+│ │ │ └── outputs.tf
+│ │ └── README.md
+│ │
+│ ├── kubernetes/ # Kubernetes manifests
+│ │ ├── base/ # Base configs
+│ │ │ ├── namespaces.yaml
+│ │ │ ├── rbac.yaml
+│ │ │ ├── kong-plugins.yaml # JWT auth, rate limiting plugins
+│ │ │ ├── kong-routes.yaml
+│ │ │ └── kong-health.yaml
+│ │ ├── helm-charts/
+│ │ │ ├── compliance-service/
+│ │ │ │ ├── templates/
+│ │ │ │ │ ├── deployment.yaml
+│ │ │ │ │ ├── service.yaml
+│ │ │ │ │ ├── ingress.yaml
+│ │ │ │ │ ├── hpa.yaml # Horizontal Pod Autoscaler
+│ │ │ │ │ ├── servicemonitor.yaml # Prometheus scraping
+│ │ │ │ │ └── external-secret.yaml # External Secrets Operator
+│ │ │ │ ├── values.yaml
+│ │ │ │ ├── values-staging.yaml
+│ │ │ │ └── Chart.yaml
+│ │ │ ├── carbon-accounting-service/
+│ │ │ │ └── ... (same structure)
+│ │ │ ├── document-processing-service/
+│ │ │ ├── risk-assessment-service/
+│ │ │ ├── supply-chain-service/
+│ │ │ ├── notification-service/
+│ │ │ └── kong-gateway/
+│ │ │ └── ... (same structure)
+│ │ └── argocd/ # GitOps application definitions
+│ │ ├── staging/
+│ │ │ ├── compliance-app.yaml
+│ │ │ ├── carbon-app.yaml
+│ │ │ ├── document-app.yaml
+│ │ │ ├── risk-app.yaml
+│ │ │ ├── supply-chain-app.yaml
+│ │ │ └── kong-app.yaml
+│ │ └── production/
+│ │ └── ... (same structure)
+│
+├── docs/ # Documentation
+│ ├── architecture/ # Architecture blueprints
+│ │ ├── 01_Context_and_Drivers.md
+│ │ ├── 02_Architecture_Overview.md
+│ │ ├── 03_System_Structure_and_Data.md
+│ │ ├── 04_Behavior_and_Communication.md
+│ │ ├── 05_Operational_Architecture.md
+│ │ ├── 06_Rationale_and_Future.md
+│ │ ├── architecture_manifest.json
+│ │ └── README.md
+│ ├── diagrams/ # PlantUML diagrams
+│ │ ├── c4-context.puml # System context (external actors)
+│ │ ├── c4-container.puml # Container diagram (microservices, DBs)
+│ │ ├── c4-component-docprocessing.puml # Document Service internals
+│ │ ├── erd-data-model.puml # Entity-Relationship Diagram
+│ │ ├── seq-document-upload.puml # Sequence: Document workflow
+│ │ ├── seq-cbam-calculation.puml # Sequence: CBAM calculation
+│ │ └── deployment-aws.puml # AWS infrastructure deployment
+│ ├── adr/ # Architectural Decision Records
+│ │ ├── 001-event-driven-architecture.md
+│ │ ├── 002-polyglot-persistence.md
+│ │ ├── 003-external-llm-apis.md
+│ │ └── README.md
+│ ├── api-specs/
+│ │ └── api-overview.md # API documentation overview
+│ └── runbooks/
+│ ├── deploy-staging.md # Staging deployment procedure
+│ ├── disaster-recovery.md # DR procedures (RTO 4h, RPO 1h)
+│ └── incident-response.md # Incident handling playbook
+│
+├── api/ # OpenAPI Specifications
+│ ├── openapi-v1.yaml # Consolidated spec (all services)
+│ ├── compliance-service.yaml
+│ ├── carbon-accounting-service.yaml
+│ ├── document-processing-service.yaml
+│ ├── risk-assessment-service.yaml
+│ └── supply-chain-service.yaml
+│
+├── shared/ # Shared libraries
+│ ├── typescript-client/ # Auto-generated API client
+│ │ ├── src/
+│ │ │ ├── apis/ # ComplianceApi, CarbonAccountingApi, etc.
+│ │ │ ├── models/ # Type definitions (ComplianceChecklist, Product, etc.)
+│ │ │ └── index.ts
+│ │ ├── package.json
+│ │ ├── tsconfig.json
+│ │ └── README.md
+│ ├── python-common/ # Shared Python utilities
+│ │ ├── sustaina_common/
+│ │ │ ├── __init__.py
+│ │ │ ├── database.py # SQLAlchemy session factory
+│ │ │ ├── logging.py # Structured JSON logging
+│ │ │ ├── config.py # Environment variable loader
+│ │ │ └── models.py # BaseModel (id, company_id, timestamps)
+│ │ ├── tests/
+│ │ ├── setup.py
+│ │ ├── requirements.txt
+│ │ └── README.md
+│ └── node-common/ # Shared Node.js utilities
+│ ├── src/
+│ │ ├── database.ts # TypeORM DataSource factory
+│ │ ├── logging.ts # Winston JSON logger
+│ │ ├── config.ts # dotenv loader
+│ │ └── middleware.ts # JWT validation, error handling
+│ ├── tests/
+│ ├── package.json
+│ ├── tsconfig.json
+│ └── README.md
+│
+├── scripts/ # Utility scripts
+│ ├── schema.sql # PostgreSQL DDL (15 tables, indexes)
+│ ├── emission-factors-schema.sql
+│ ├── mongodb-schemas.json # RegulatoryFramework JSON Schema
+│ ├── seed-emission-factors.py # Load 30+ emission factors (DEFRA, EPA)
+│ ├── seed-regulatory-frameworks.py # Load CBAM, ISO 14064, GHG Protocol
+│ ├── generate-test-data.py # Generate sample SME data
+│ ├── deploy-web-app.sh # Web app deployment script
+│ ├── backup-databases.sh
+│ ├── emission-data/
+│ │ └── defra-2024-factors.csv
+│ ├── frameworks-data/
+│ │ ├── cbam-eu-2026.json
+│ │ ├── iso-14064.json
+│ │ └── ghg-protocol.json
+│ └── migrations/
+│ └── 001_initial_schema.sql
+│
+├── tests/ # End-to-end & integration tests
+│ ├── e2e/ # Playwright tests
+│ │ ├── compliance-workflow.spec.ts # Generate checklist → upload docs → view risk
+│ │ ├── carbon-calculation.spec.ts # Create product → add suppliers → calculate
+│ │ └── cbam-report.spec.ts # Generate CBAM report → download PDF
+│ ├── integration/ # Cross-service tests
+│ │ ├── fixtures/
+│ │ │ ├── sample-invoice.pdf
+│ │ │ └── sample-epd.xlsx
+│ │ ├── test_document_workflow.py # Document upload → extraction → validation
+│ │ └── test_calculation_accuracy.py # Verify emission calculations
+│ └── postman/
+│ └── sustaina-api.postman_collection.json
+│
+├── .codemachine/ # CodeMachine orchestration
+│ ├── inputs/
+│ │ └── specifications.md # Source requirements (187 pages)
+│ ├── artifacts/
+│ │ ├── architecture/ # Generated blueprints
+│ │ │ └── ... (6 documents)
+│ │ ├── plan/ # Iteration plans
+│ │ │ └── ... (7 documents)
+│ │ └── tasks/ # Task specifications
+│ │ └── ... (5 JSON files, 39 tasks)
+│ ├── prompts/
+│ │ ├── context.md
+│ │ ├── plan_fallback.md
+│ │ ├── task_fallback.md
+│ │ └── code_fallback.md
+│ ├── agents/
+│ │ └── agents-config.json
+│ └── template.json
+│
+├── .gitignore
+├── .dockerignore
+├── .pre-commit-config.yaml # Pre-commit hooks (linters, formatters)
+├── .eslintrc.js
+├── .prettierrc
+├── package.json # Root workspace (monorepo)
+├── package-lock.json
+├── openapitools.json # OpenAPI Generator config
+├── README.md
+├── CONTRIBUTING.md
+└── LICENSE
+```
+
+**Directory Statistics:**
+- **Total Directories:** 147
+- **Total Files:** 482
+- **Lines of Code:** 60,008 (excluding dependencies)
+
+**Code Breakdown by Language:**
+```
+Language Files Code Lines
+JSON 44 21,155
+Python 118 11,915
+Markdown 42 8,910
+TypeScript 99 6,236
+YAML 83 5,716
+HCL (Terraform) 19 1,693
+PlantUML 8 840
+JavaScript 22 936
+Shell Scripts 9 879
+HTML 7 938
+SQL 5 473
+Dockerfile 7 139
+Other 19 178
+```
+
+---
+
+## 7. Key Implementation Patterns
+
+### 7.1 Multi-Tenancy Pattern
+
+**Row-Level Security with Partition Key:**
+```python
+# shared/python-common/sustaina_common/models.py
+from sqlalchemy import Column, UUID, TIMESTAMP
+from sqlalchemy.ext.declarative import declarative_base
+
+Base = declarative_base()
+
+class BaseModel(Base):
+ __abstract__ = True
+
+ id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
+ company_id = Column(UUID(as_uuid=True), nullable=False, index=True) # Partition key
+ created_at = Column(TIMESTAMP, default=func.now())
+ updated_at = Column(TIMESTAMP, default=func.now(), onupdate=func.now())
+```
+
+**Automatic Filtering:**
+```python
+# services/compliance-service/src/api/checklists.py
+from fastapi import Depends, HTTPException
+from shared.python_common.auth import get_current_user
+
+@router.get("/checklists")
+def get_checklists(
+ user: User = Depends(get_current_user),
+ db: Session = Depends(get_db)
+):
+ # Automatically filter by user's company_id
+ checklists = db.query(ComplianceChecklist).filter(
+ ComplianceChecklist.company_id == user.company_id
+ ).all()
+ return checklists
+```
+
+### 7.2 Event-Driven Workflow Pattern
+
+**Document Processing Workflow:**
+```python
+# services/document-processing-service/src/workers/ocr_worker.py
+import boto3
+
+sqs = boto3.client('sqs')
+sns = boto3.client('sns')
+
+def process_document_upload_event(event):
+ """
+ Consumes: DocumentUploaded event
+ Publishes: DocumentTextExtracted event
+ """
+ document_id = event['document_id']
+ s3_key = event['s3_key']
+
+ # Step 1: Download from S3
+ document_bytes = s3_client.get_object(Bucket='sustaina-documents', Key=s3_key)['Body'].read()
+
+ # Step 2: OCR
+ if s3_key.endswith('.pdf'):
+ text = extract_text_from_pdf(document_bytes) # PyPDF2
+ else:
+ text = extract_text_from_image(document_bytes) # PyTesseract
+
+ # Step 3: Store extracted text
+ db.query(Document).filter(Document.id == document_id).update({'extracted_text': text})
+ db.commit()
+
+ # Step 4: Publish event for LLM extraction
+ sns.publish(
+ TopicArn='arn:aws:sns:eu-central-1:123456789:document-text-extracted',
+ Message=json.dumps({'document_id': document_id, 'text': text})
+ )
+```
+
+### 7.3 AI Extraction Pattern with Confidence Scoring
+
+**LLM Pipeline:**
+```python
+# services/document-processing-service/src/core/llm_pipeline.py
+from langchain import PromptTemplate, LLMChain
+from langchain.llms import OpenAI
+
+def extract_invoice_fields(text: str) -> dict:
+ """
+ Extract structured fields from invoice text using GPT-4
+ Returns: {fields: dict, confidence_scores: dict}
+ """
+ prompt = PromptTemplate(
+ input_variables=["text"],
+ template="""
+ Extract the following fields from this invoice:
+ - supplier_name
+ - supplier_location (country)
+ - total_amount (numeric value only)
+ - invoice_date (ISO 8601 format)
+ - line_items (array of {description, quantity, unit_price})
+
+ Respond ONLY with valid JSON. For each field, provide a confidence score (0-1).
+
+ Invoice text:
+ {text}
+
+ JSON:
+ """
+ )
+
+ llm = OpenAI(model="gpt-4", temperature=0) # Low temp for consistency
+ chain = LLMChain(llm=llm, prompt=prompt)
+
+ result = chain.run(text=text)
+ parsed = json.loads(result)
+
+ # Separate fields and confidence scores
+ fields = {k: v['value'] for k, v in parsed.items()}
+ confidence_scores = {k: v['confidence'] for k, v in parsed.items()}
+
+ return fields, confidence_scores
+```
+
+**Human-in-the-Loop for Low Confidence:**
+```python
+# services/document-processing-service/src/core/validator.py
+def validate_extraction(document_id: str, fields: dict, confidence_scores: dict):
+ """
+ Flag extractions with low confidence for human review
+ """
+ CONFIDENCE_THRESHOLD = 0.8
+
+ low_confidence_fields = [
+ field for field, score in confidence_scores.items()
+ if score < CONFIDENCE_THRESHOLD
+ ]
+
+ if low_confidence_fields:
+ # Create review task
+ db.add(ReviewTask(
+ document_id=document_id,
+ status='pending_review',
+ flagged_fields=low_confidence_fields,
+ message=f"Low confidence on: {', '.join(low_confidence_fields)}"
+ ))
+ db.commit()
+
+ # Notify admin
+ sns.publish(
+ TopicArn='arn:aws:sns:eu-central-1:123456789:admin-review-required',
+ Message=f"Document {document_id} requires human review"
+ )
+```
+
+### 7.4 Carbon Calculation Pattern
+
+**GHG Protocol Implementation:**
+```python
+# services/carbon-accounting-service/src/core/ghg_calculator.py
+from decimal import Decimal
+
+class GHGCalculator:
+ def calculate_product_emissions(self, product_id: UUID) -> EmissionCalculation:
+ """
+ Calculate Scope 1-3 emissions for a product
+ """
+ # Fetch product with suppliers
+ product = db.query(Product).filter(Product.id == product_id).first()
+ suppliers = product.suppliers # Many-to-many relationship
+
+ # Scope 1: Direct manufacturing emissions
+ scope1 = self._calculate_scope1(product)
+
+ # Scope 2: Purchased energy emissions
+ scope2 = self._calculate_scope2(product)
+
+ # Scope 3: Supply chain emissions (recursive)
+ scope3 = self._calculate_scope3(product, suppliers)
+
+ total_co2e = scope1 + scope2 + scope3
+
+ # Data quality score (higher if using supplier-specific data vs defaults)
+ quality_score = self._calculate_data_quality(suppliers)
+
+ # Save calculation
+ calculation = EmissionCalculation(
+ product_id=product_id,
+ scope1_co2e=scope1,
+ scope2_co2e=scope2,
+ scope3_co2e=scope3,
+ total_co2e=total_co2e,
+ data_quality_score=quality_score,
+ calculation_date=datetime.utcnow()
+ )
+ db.add(calculation)
+ db.commit()
+
+ return calculation
+
+ def _calculate_scope3(self, product: Product, suppliers: List[Supplier]) -> Decimal:
+ """
+ Aggregate supplier emissions (multi-tier supply chain)
+ """
+ scope3_total = Decimal(0)
+
+ for supplier in suppliers:
+ # Get product-supplier link (quantity used)
+ link = db.query(ProductSupplier).filter(
+ ProductSupplier.product_id == product.id,
+ ProductSupplier.supplier_id == supplier.id
+ ).first()
+
+ quantity = link.quantity
+
+ # Lookup emission factor
+ emission_factor = self._get_emission_factor(
+ region=supplier.location,
+ sector=supplier.industry_sector,
+ activity=link.activity # e.g., "cement production"
+ )
+
+ # Calculate emissions for this supplier
+ supplier_emissions = quantity * emission_factor.co2e_per_unit
+
+ scope3_total += supplier_emissions
+
+ return scope3_total
+
+ def _get_emission_factor(self, region: str, sector: str, activity: str) -> EmissionFactor:
+ """
+ Fetch emission factor with Redis caching
+ """
+ cache_key = f"ef:{region}:{sector}:{activity}"
+
+ # Try cache first
+ cached = redis.get(cache_key)
+ if cached:
+ return EmissionFactor(**json.loads(cached))
+
+ # Query database
+ factor = db.query(EmissionFactor).filter(
+ EmissionFactor.region == region,
+ EmissionFactor.industry_sector == sector,
+ EmissionFactor.activity == activity
+ ).first()
+
+ if not factor:
+ # Substitution logic: fallback to default factor
+ factor = self._get_default_emission_factor(sector, activity)
+
+ # Cache for 30 days
+ redis.setex(cache_key, 30 * 24 * 3600, json.dumps(factor.to_dict()))
+
+ return factor
+```
+
+### 7.5 Infrastructure as Code Pattern
+
+**Terraform Module for S3 + CloudFront:**
+```hcl
+# infrastructure/terraform/modules/s3-web-hosting/main.tf
+resource "aws_s3_bucket" "web_app" {
+ bucket = "sustaina-web-app-${var.environment}"
+
+ tags = {
+ Name = "Sustaina Web App"
+ Environment = var.environment
+ }
+}
+
+resource "aws_s3_bucket_public_access_block" "web_app" {
+ bucket = aws_s3_bucket.web_app.id
+
+ block_public_acls = true # Security: No public ACLs
+ block_public_policy = true
+ ignore_public_acls = true
+ restrict_public_buckets = true
+}
+
+resource "aws_s3_bucket_website_configuration" "web_app" {
+ bucket = aws_s3_bucket.web_app.id
+
+ index_document {
+ suffix = "index.html"
+ }
+
+ error_document {
+ key = "index.html" # SPA routing
+ }
+}
+
+# CloudFront Origin Access Control (OAC)
+resource "aws_cloudfront_origin_access_control" "web_app" {
+ name = "sustaina-web-app-${var.environment}"
+ origin_access_control_origin_type = "s3"
+ signing_behavior = "always"
+ signing_protocol = "sigv4"
+}
+
+# ACM Certificate (must be in us-east-1 for CloudFront)
+resource "aws_acm_certificate" "web_app" {
+ provider = aws.us_east_1 # Alias provider
+ domain_name = var.domain_name
+ validation_method = "DNS"
+
+ lifecycle {
+ create_before_destroy = true
+ }
+}
+
+# CloudFront Distribution
+resource "aws_cloudfront_distribution" "web_app" {
+ enabled = true
+ is_ipv6_enabled = true
+ default_root_object = "index.html"
+ aliases = [var.domain_name]
+
+ origin {
+ domain_name = aws_s3_bucket.web_app.bucket_regional_domain_name
+ origin_id = "S3-${aws_s3_bucket.web_app.id}"
+ origin_access_control_id = aws_cloudfront_origin_access_control.web_app.id
+ }
+
+ default_cache_behavior {
+ allowed_methods = ["GET", "HEAD", "OPTIONS"]
+ cached_methods = ["GET", "HEAD"]
+ target_origin_id = "S3-${aws_s3_bucket.web_app.id}"
+
+ forwarded_values {
+ query_string = false
+ cookies {
+ forward = "none"
+ }
+ }
+
+ viewer_protocol_policy = "redirect-to-https"
+ min_ttl = 0
+ default_ttl = 0 # index.html not cached
+ max_ttl = 0
+ }
+
+ # Cache static assets (JS, CSS) for 1 year
+ ordered_cache_behavior {
+ path_pattern = "/assets/*"
+ allowed_methods = ["GET", "HEAD"]
+ cached_methods = ["GET", "HEAD"]
+ target_origin_id = "S3-${aws_s3_bucket.web_app.id}"
+
+ forwarded_values {
+ query_string = false
+ cookies {
+ forward = "none"
+ }
+ }
+
+ viewer_protocol_policy = "redirect-to-https"
+ min_ttl = 31536000 # 1 year
+ default_ttl = 31536000
+ max_ttl = 31536000
+ }
+
+ viewer_certificate {
+ acm_certificate_arn = aws_acm_certificate.web_app.arn
+ ssl_support_method = "sni-only"
+ minimum_protocol_version = "TLSv1.2_2021"
+ }
+
+ restrictions {
+ geo_restriction {
+ restriction_type = "none"
+ }
+ }
+
+ logging_config {
+ bucket = aws_s3_bucket.cloudfront_logs.bucket_domain_name
+ prefix = "web-app/"
+ }
+}
+
+# Route 53 DNS Record
+resource "aws_route53_record" "web_app" {
+ zone_id = var.route53_zone_id
+ name = var.domain_name
+ type = "A"
+
+ alias {
+ name = aws_cloudfront_distribution.web_app.domain_name
+ zone_id = aws_cloudfront_distribution.web_app.hosted_zone_id
+ evaluate_target_health = false
+ }
+}
+
+# Outputs for CI/CD
+output "distribution_id" {
+ value = aws_cloudfront_distribution.web_app.id
+ description = "CloudFront distribution ID for cache invalidation"
+}
+
+output "bucket_name" {
+ value = aws_s3_bucket.web_app.id
+ description = "S3 bucket name for deployment"
+}
+```
+
+---
+
+## 8. Results & Deliverables
+
+### 8.1 Completed Deliverables
+
+**Infrastructure & DevOps:**
+- ✅ 5 Terraform modules (VPC, EKS, RDS, ElastiCache, S3-Web-Hosting)
+- ✅ 7 Helm charts (7 microservices)
+- ✅ 6 ArgoCD application definitions
+- ✅ 4 GitHub Actions workflows (CI backend, CI frontend, CD staging, CD web-app)
+- ✅ Multi-region deployment (EU-Central-1, ME-South-1)
+- ✅ Blue-green deployment infrastructure
+
+**Backend Services:**
+- ✅ 7 microservices (Compliance, Carbon Accounting, Document Processing, Risk Assessment, Supply Chain, ESG Reporting, Notification)
+- ✅ 42 API endpoints (RESTful, OpenAPI 3.1 documented)
+- ✅ PostgreSQL schema (15 tables, 32 indexes, row-level security)
+- ✅ MongoDB regulatory framework schemas (CBAM, ISO 14064, GHG Protocol)
+- ✅ Redis caching layer (emission factors, API responses)
+- ✅ SQS/SNS event-driven workflows (document processing, calculations)
+- ✅ AI document processing pipeline (PyTesseract OCR + GPT-4 extraction)
+- ✅ GHG Protocol carbon calculation engine (Scopes 1-3)
+- ✅ CBAM report generation (PDF with standardized templates)
+- ✅ Traffic-light risk scoring algorithm
+
+**Frontend:**
+- ✅ React 18 web application (TypeScript)
+- ✅ 5 feature modules (Compliance, Carbon, Documents, Risk, Auth)
+- ✅ 15+ reusable UI components (Button, Card, Modal, Table, etc.)
+- ✅ React Query for server state management
+- ✅ Tailwind CSS design system
+- ✅ i18next internationalization (English, Arabic)
+- ✅ Deployed to S3 + CloudFront CDN
+
+**Testing & Quality:**
+- ✅ Unit tests (>80% coverage across services)
+- ✅ Integration tests (cross-service workflows)
+- ✅ Playwright E2E tests (compliance workflow, carbon calculation, CBAM report)
+- ✅ Postman collection (45 API requests with examples)
+- ✅ Load testing reports (k6: 1000 concurrent users, <60s response time)
+- ✅ OWASP ZAP security scan (0 high-severity vulnerabilities)
+
+**Documentation:**
+- ✅ 6 architecture blueprint documents (187 pages)
+- ✅ 7 PlantUML diagrams (Context, Container, Component, ERD, 2 Sequence, Deployment)
+- ✅ 6 OpenAPI 3.1 specifications (5 services + consolidated)
+- ✅ 3 Architectural Decision Records (ADRs)
+- ✅ 4 operational runbooks (deploy, disaster recovery, incident response)
+- ✅ Auto-generated API documentation (Swagger UI, Redoc)
+
+**Data & Configuration:**
+- ✅ 30+ emission factors (DEFRA, EPA datasets)
+- ✅ 3 regulatory framework definitions (CBAM, ISO 14064, GHG Protocol)
+- ✅ Sample test data (5 SME companies, 20 products, 50 suppliers)
+
+### 8.2 Visual Deliverables
+
+**C4 Architecture Diagrams:**
+1. **System Context Diagram:** Shows Sustaina platform boundary with external actors (SME users, auditors, supply chain managers) and systems (LLM providers, emission databases, Auth0, email service)
+2. **Container Diagram:** Illustrates 7 microservices, API Gateway, Web App, 4 databases (PostgreSQL, MongoDB, Redis, S3), message queue (SQS/SNS), search engine (Elasticsearch)
+3. **Component Diagram (Document Processing Service):** Details OCR Engine, LLM Pipeline, Validation Engine, Evidence Repository, Event Publisher
+4. **Deployment Diagram:** AWS infrastructure across 3 availability zones (VPC, subnets, ALB, EKS nodes, RDS Multi-AZ, ElastiCache, S3, SQS)
+
+**Data Model:**
+- **ERD:** 15 entities with relationships (Company → Users, Products → Suppliers, Documents → Extractions, Checklists → Items)
+
+**Workflow Diagrams:**
+1. **Document Upload Sequence:** User → Upload → OCR → LLM Extraction → Validation → Risk Update → Notification
+2. **CBAM Calculation Sequence:** User → Calculation Request → Supplier Fetch → Emission Factor Lookup → Calculate → Report Generation → Notification
+
+### 8.3 Code Quality Metrics
+
+| Metric | Target | Achieved |
+|--------|--------|----------|
+| **Test Coverage** | >80% | 84% (Backend), 78% (Frontend) |
+| **API Response Time** | <60s (calculations) | 42s average (100 suppliers) |
+| **AI Extraction Accuracy** | >90% (structured), >80% (semi-structured) | 94% (invoices), 83% (scanned PDFs) |
+| **Uptime (Staging)** | 99.5% | 99.7% (30-day average) |
+| **Build Time (CI)** | <10 minutes | 7.5 minutes (parallel jobs) |
+| **Docker Image Size** | <500MB per service | 280MB average (multi-stage builds) |
+| **OpenAPI Compliance** | 100% endpoints documented | 100% (42/42 endpoints) |
+| **Security Vulnerabilities** | 0 critical/high | 0 high, 2 medium (false positives) |
+
+---
+
+## 9. Technical Metrics
+
+### 9.1 Development Velocity & Automation Metrics
+
+**Total Project Timeline:** 10 weeks (Fully automated development)
+
+**Phase-by-Phase Breakdown:**
+
+| Phase | Time Investment | Key Deliverables |
+|-------|----------------|------------------|
+| **Architecture Planning** | 30 minutes | System architecture, C4 diagrams, ERDs, technical design decisions |
+| **Service Implementation** | 5 hours | 7 microservices with 42 API endpoints, 60,008 lines of code across 482 files |
+| **Integration & Testing** | 2 hours | Automated validation, unit tests, integration tests, E2E workflows |
+| **Deployment Setup** | 30 minutes | Terraform modules, Helm charts, CI/CD pipelines, runtime automation scripts |
+| **Total Active Development** | ~8 hours | Complete production-ready platform |
+
+**Development Efficiency:**
+- **Efficiency Gain:** 25-37× faster than traditional development
+- **Code Consistency:** Unified architecture and patterns across all 7 microservices
+- **Quality Control:** Built-in validation at each step with automated sanity checks
+- **Context Retention:** Full project context maintained with cross-service awareness throughout development
+
+**Code Generation Metrics:**
+- **Lines of Code Generated:** 60,008 (482 files)
+- **Automated Code Distribution:**
+ - JSON configurations: 21,155 lines
+ - Python services: 11,915 lines
+ - TypeScript/JavaScript: 7,172 lines
+ - Infrastructure (YAML + HCL): 7,409 lines
+ - Documentation: 8,910 lines
+
+### 9.2 Infrastructure Metrics
+
+**AWS Resources Provisioned (Staging Environment):**
+- **Compute:**
+ - 1 EKS cluster (Kubernetes 1.28)
+ - 6 EC2 instances (t3.large) across 3 availability zones
+ - Auto-scaling group (min 2, max 10 nodes)
+- **Databases:**
+ - 1 RDS PostgreSQL 15 (db.r6g.xlarge, Multi-AZ)
+ - 1 ElastiCache Redis 7 cluster (2 nodes, cross-AZ replication)
+ - 1 MongoDB Atlas cluster (M10 tier, 3-node replica set)
+- **Storage:**
+ - 3 S3 buckets (documents, backups, CloudFront logs)
+ - Total storage: 45 GB (documents: 30 GB, backups: 12 GB, logs: 3 GB)
+- **Networking:**
+ - 1 VPC (10.0.0.0/16)
+ - 6 subnets (3 public, 3 private)
+ - 2 NAT Gateways
+ - 1 Application Load Balancer
+ - 1 CloudFront distribution
+- **Message Queue:**
+ - 5 SQS queues (document-processing, calculation-requests, notifications, risk-updates, dead-letter)
+ - 3 SNS topics (document-events, calculation-events, admin-alerts)
+
+### 9.3 Performance Benchmarks
+
+**API Response Times (k6 Load Testing):**
+- **Checklist Generation:** 1.2s average (500 concurrent users)
+- **Product Emission Calculation:** 42s average (100 suppliers), 18s (20 suppliers)
+- **Document Upload (Presigned URL):** 0.3s
+- **Document Extraction (Async):** 8-12s (OCR + LLM pipeline)
+- **Risk Score Retrieval:** 0.8s (with Redis cache), 2.4s (cache miss)
+
+**Database Query Performance:**
+- **Emission Factor Lookup:** 12ms (indexed query on region + sector + activity)
+- **Supplier List (Pagination):** 35ms (1000 suppliers, cursor-based pagination)
+- **Audit Log Write:** 8ms (append-only table, no indexes on write path)
+- **MongoDB Framework Query:** 18ms (indexed on jurisdiction)
+
+**AI Processing Performance:**
+- **OCR (PyTesseract):** 3-5s per scanned PDF page
+- **GPT-4 Extraction:** 4-8s per document (latency depends on OpenAI API)
+- **Validation Engine:** 0.5s (ruleset matching against 50 regulatory requirements)
+
+### 9.4 Scalability Metrics
+
+**Horizontal Pod Autoscaling (HPA) Configuration:**
+- **Compliance Service:** Min 2, Max 8 replicas (CPU threshold: 70%)
+- **Carbon Accounting Service:** Min 2, Max 10 replicas (CPU threshold: 75%, high compute for calculations)
+- **Document Processing Service:** Min 3, Max 12 replicas (Memory threshold: 80%, high memory for OCR/LLM)
+- **Risk Assessment Service:** Min 2, Max 6 replicas
+- **Supply Chain Service:** Min 2, Max 6 replicas
+- **Notification Service:** Min 2, Max 5 replicas
+
+**Concurrent User Testing:**
+- **Test Scenario:** 1000 concurrent users performing mixed operations (checklist retrieval, document uploads, calculations)
+- **Result:** Average response time: 1.8s, 99th percentile: 5.2s, 0.02% error rate (all timeouts, no crashes)
+
+---
+
+## Conclusion
+
+The Sustaina ESG Compliance Platform represents a successful demonstration of **AI-orchestrated software development** at enterprise scale. CodeMachine's multi-agent architecture transformed a 187-page specification into a production-ready system with:
+
+- **7 microservices** handling complex regulatory logic, AI document processing, and carbon accounting
+- **Multi-database architecture** (PostgreSQL, MongoDB, Redis, Elasticsearch) optimized for different data patterns
+- **Cloud-native infrastructure** (AWS EKS, RDS, S3, CloudFront) with blue-green deployment
+- **Comprehensive testing** (unit, integration, E2E) achieving 84% backend and 78% frontend coverage, with key calculation SLAs of 42s (against a 60s target)
+- **Full CI/CD automation** (GitHub Actions, ArgoCD) enabling daily deployments to staging
+
+**Key Innovations:**
+1. **Hierarchical Agent Orchestration:** 48 specialized agents coordinated through bidirectional communication, reducing development time by an estimated 75% compared to manual implementation
+2. **Context-Aware Code Generation:** Dynamic injection of architecture blueprints and existing code patterns ensured consistency and reduced hallucinations
+3. **Verification-Driven Quality:** Automated validation loops (OpenAPI, SQL, TypeScript compilation) caught 87% of errors pre-review
+4. **Artifact-First Development:** PlantUML diagrams and OpenAPI specs served as executable documentation, staying synchronized with code
+
+**Impact for SMEs:**
+- **Democratized Compliance:** Complex CBAM, ESRS, ISO regulations distilled into clear checklists and traffic-light risk indicators
+- **Reduced Compliance Costs:** Estimated 60% cost reduction vs manual consultants (€5,000/year vs €12,000-15,000)
+- **Faster Market Access:** CBAM reports generated in minutes vs weeks of manual calculation
+- **Audit Readiness:** AI-verified documentation provides defensible compliance evidence
+
+CodeMachine has proven that **specification-to-code orchestration** can deliver enterprise-grade systems when:
+- Specifications are detailed and structured (architecture blueprints, acceptance criteria)
+- Agents are specialized by domain (database, backend, frontend, DevOps)
+- Verification loops validate artifacts continuously
+- Human oversight focuses on strategic decisions rather than boilerplate code
+
+As Sustaina scales to serve thousands of SMEs across the MENA region, the CodeMachine-generated foundation provides a robust, maintainable, and extensible platform for continuous evolution of ESG compliance requirements.
+
+
diff --git a/docs/cli-reference.md b/docs/cli-reference.md
new file mode 100644
index 00000000..b94d6ccc
--- /dev/null
+++ b/docs/cli-reference.md
@@ -0,0 +1,704 @@
+# CLI Reference
+
+Complete command-line interface reference for CodeMachine.
+
+## Overview
+
+CodeMachine provides a command-line interface for managing workflows, executing agents, and configuring your development environment.
+
+
+**Basic Usage:**
+```bash
+codemachine [command] [options]
+```
+
+**Global Options:**
+
+| Option | Description | Default |
+|--------|-------------|---------|
+| `-d, --dir ` | Target workspace directory | `process.cwd()` |
+| `--spec ` | Path to planning specification file | `.codemachine/inputs/specifications.md` |
+| `-h, --help` | Display help for command | - |
+
+**Package Binary:**
+- Entry point: `./dist/index.js`
+- Command name: `codemachine`
+
+---
+
+## Interactive Mode
+
+When no command is provided, CodeMachine starts in interactive session mode.
+
+**Usage:**
+```bash
+codemachine
+codemachine -d /path/to/workspace
+```
+
+**Features:**
+- Interactive shell session with keyboard controls
+- Real-time workflow execution
+- Template selection menu
+- Authentication management
+- Onboarding for new users
+
+**Session Flow:**
+1. CLI checks working directory (`-d` option or current directory)
+2. Syncs configuration for all registered engines
+3. Bootstraps `.codemachine/` folder if it doesn't exist
+4. Enters interactive shell with main menu
+
+**Workspace Structure:**
+```
+.codemachine/
+├── inputs/
+│ └── specifications.md # Default spec file
+├── template.json # Selected template
+└── [engine-specific-configs]
+```
+
+---
+
+## Workflow Commands
+
+Commands for managing and executing workflows.
+
+### `start`
+
+Run the workflow queue until completion in non-interactive mode.
+
+**Syntax:**
+```bash
+codemachine start [options]
+```
+
+**Options:**
+
+| Option | Description | Default |
+|--------|-------------|---------|
+| `--spec ` | Path to the planning specification file | `.codemachine/inputs/specifications.md` |
+
+**Behavior:**
+- Executes workflow queue sequentially
+- Runs in non-interactive mode (no user prompts)
+- Exits with status code on completion
+
+**Exit Codes:**
+- `0` - Workflow completed successfully
+- `1` - Workflow failed
+
+**Output Messages:**
+- Success: `✓ Workflow completed successfully`
+- Error: `✗ Workflow failed: [error message]`
+
+**Examples:**
+```bash
+# Run workflow with default spec
+codemachine start
+
+# Run workflow with custom spec
+codemachine start --spec ./custom/planning.md
+
+# Run in specific directory
+codemachine -d /path/to/project start
+
+# Custom directory and spec
+codemachine -d /path/to/project start --spec ./specs/feature.md
+```
+
+**Use Cases:**
+- CI/CD pipeline automation
+- Batch workflow execution
+- Automated code generation scripts
+- Testing workflows
+
+**Technical Details:**
+- Source: `src/cli/commands/start.command.ts`
+- Non-blocking execution
+- Error handling with detailed messages
+
+---
+
+### `templates`
+
+List and select workflow templates interactively.
+
+**Syntax:**
+```bash
+codemachine templates
+```
+
+**Arguments:** None
+
+**Options:** None
+
+**Behavior:**
+- Lists all available workflow templates from `templates/workflows/`
+- Displays interactive selection menu
+- Auto-regenerates agents folder when template changes
+- Saves selection to `.codemachine/template.json`
+
+**Template Format:**
+- Files ending with `.workflow.js`
+- Located in `templates/workflows/` directory
+- Export workflow configuration and agent definitions
+
+**Examples:**
+```bash
+# List and select template interactively
+codemachine templates
+
+# Use in specific workspace
+codemachine -d /path/to/project templates
+```
+
+**Template Storage:**
+- Selection saved to: `.codemachine/template.json`
+- Default template: `templates/workflows/codemachine.workflow.js`
+- Example template: `templates/workflows/_example.workflow.js`
+
+**Use Cases:**
+- Switch between different workflow types
+- Initialize new projects with specific templates
+- Customize agent configurations per project
+
+**Technical Details:**
+- Source: `src/cli/commands/templates.command.ts`
+- Supports both interactive and programmatic selection
+- Triggers agent folder regeneration on template change
+
+---
+
+## Development Commands
+
+Commands for executing agents and workflow steps during development.
+
+### `run`
+
+Execute single agents or orchestrate multiple agents with the unified run command.
+
+**Syntax:**
+```bash
+codemachine run