I Built a Framework to Tame AI-Generated Spaghetti Code

Discover how the Flow framework tames AI-generated spaghetti code by structuring your project as a modular monolith. No more fixing Copilot errors.

lunes, 27 de julio de 2026 • 6 min read • Q2BSTUDIO Team

Cómo estructurar tu repositorio para que la IA no se equivoque

The rise of code assistants powered by artificial intelligence has transformed how developers approach their projects. Tools like GitHub Copilot promise to accelerate code writing, but often generate silent chaos: scattered files, incorrect imports, inconsistent patterns, and a constant feeling of fighting against the machine. Anyone who has used these assistants for more than a week knows the frustration of spending twenty minutes fixing what the AI broke, instead of advancing the actual functionality. At Q2BSTUDIO, a company specialized in custom software development, we know the key is not to reject AI but to design environments where it cannot make mistakes. That is why we have explored and implemented an architecture we call 'AI-oriented modular monolith', an approach that tames the spaghetti code generated by artificial intelligence and turns human-machine collaboration into a productive experience.

The root of the problem is structural. Most traditional repositories organize code by file type: a directory for backend handlers, another for services, another for models, and then separate folders for frontend, components, and tests. When a developer asks the AI to add a new module, the assistant lacks enough context to know where to place each piece. It ends up writing a handler in the wrong folder, importing modules it should not touch, and generating frontend code that does not follow existing conventions. The result is an incoherent mosaic that requires exhaustive manual review. The solution is not to improve the AI, but to organize the repository so that the AI can see the complete picture of each functionality in one place.

This is where the concept of colocation comes in: one feature, one folder. Instead of spreading a module's files across seven different directories, everything related to that feature — handlers, services, repositories, models, React components, API client, configuration, tests — resides inside modules//. This pattern drastically reduces ambiguity. The AI, by opening a single directory, understands the full scope of the feature and can generate coherent code without guesswork. But colocation alone is not enough. We need a system of clear rules that the assistant reads automatically.

Instructions for the AI, in markdown format inside the repository (for example, .github/copilot-instructions.md in VS Code), allow defining precise rules: 'All code for a feature must be in modules//', 'One module must not directly import another module's package', 'React components shared by two or more modules go in the root components/ folder'. These rules act as containment barriers. Copilot follows them with surprising fidelity because it is not asked to reason, but to obey explicit instructions. It is like giving a map to a novice driver: even if they do not know the route by heart, clear directions prevent them from getting lost.

Another pillar of this strategy is automating repetitive tasks through scripts. Instead of letting the AI hallucinate the structure of a new module, we provide it with a scaffolding script that generates fifteen or more files — handler, service, repository, model, route registration, React component, API client, tests — all following the exact same pattern. The AI learns to invoke that script when it hears phrases like 'create a module' or 'add a table'. This way, new code generation is predictable and consistent. There is no room for creative interpretations that later need fixing.

Separating roles between AI agents also makes a notable difference. We can set up two modes: an implementer agent with read, write, and command execution permissions, and a reviewer agent that is read-only, which analyzes code without modifying it. After implementing a feature, we simply ask the reviewer to verify module boundaries, import rules, and style conventions. This eliminates the need for formal code reviews for minor changes and speeds up the development cycle. The AI does not become smarter, but it operates within a framework that forces discipline.

This approach does not replace the developer's understanding of their own code. Someone has to define the rules, design the architecture, and oversee results. But it removes the most annoying friction: time wasted fixing avoidable AI errors. Instead of fighting the assistant, the team can focus on business logic, user experience, and product quality. And when it comes to scaling solutions, well-executed modularity facilitates maintenance, testing, and independent evolution of each feature.

At Q2BSTUDIO we apply this philosophy in multiple custom software development projects, where the combination of AI and modular architecture delivers robust software in less time. Our team also integrates artificial intelligence, cybersecurity, and cloud computing capabilities to offer complete solutions. For example, when building platforms on AWS or Azure, we use specialized AI agents that respect repository rules and integrate with security and monitoring services. The same logic applies in Business Intelligence projects with Power BI: scaffolding scripts generate dashboards following predefined patterns, reducing errors and accelerating deployment. If your company is looking for cloud services with AWS and Azure, we can design development environments optimized for AI collaboration.

The key to this entire approach is understanding that artificial intelligence is not a substitute for human judgment, but a tool that needs clear boundaries to be productive. The repository structure becomes the control mechanism: directories, scripts, instruction files, and agents define a space where the AI can operate without causing havoc. It is like building a runway for an autopilot; if the runway is straight and well-marked, the landing will be smooth. If the AI is left in an open field, it will crash into the trees of technical debt.

Companies that adopt this mindset gain speed without sacrificing order. Instead of having to review every line generated by the AI, they trust that the rule and script system will maintain consistency. This is especially valuable in small teams or startups, where every minute counts. But also in large organizations, where standardizing code patterns is critical for long-term maintainability. The AI does not magically produce perfect code; it only does so when provided with the precise context and appropriate constraints.

In summary, taming the spaghetti code of AI is possible if you invest in designing the project structure to be clear, predictable, and with explicit rules. It is not an esoteric technique, but sensible software engineering applied to the new reality of assisted programming. From Q2BSTUDIO, we encourage teams to experiment with modular monoliths, to write instructions for their assistants, and to automate repetitive tasks. The result is a workflow where AI accelerates what it does well — generating boilerplate code, following patterns, executing scripts — while developers focus on what truly adds value: creativity, business logic, and product vision. And if you need support to implement this architecture or to integrate AI agents into your processes, we are here to help you build the future without leaving code quality behind.

A BREAK?

Play for a moment before you go

OUR SERVICES

How we can help you

Do you have a project in mind?

Tell us your vision and we'll turn it into a software solution. Whatever the scope, we make your idea real.