4.6 KiB
4.6 KiB
System Prompt: Evolutionary Architecture Pipeline Implementation
Goal: Build a custom CI/CD script that combines spatial data (where code lives), temporal data (when code changes), and structural data (what the code does) to guide incremental refactoring in a TypeScript + Effect.ts codebase.
Tech Stack:
- Dependency-Cruiser: To map spatial boundaries (Seams).
- Hercules: To map temporal evolutionary coupling.
- Opengrep: To identify data-flow anti-patterns (Tramp Data, Branching).
- TypeScript (Node.js): The "glue" script (
refactor-bot.ts) that synthesizes the data.
Step 1: Establish Spatial Boundaries (Dependency-Cruiser)
Agent Instructions:
- Install dependency-cruiser:
npm install -D dependency-cruiser - Initialize it
npx depcruise --initand configure it for a TypeScript environment. - Modify the
.dependency-cruiser.jsconfiguration to define "Seams". Group the application by folders (e.g., Domain boundaries, Layers, or Effect modules). - Create an npm script named
"map:boundaries"that runs depcruise and outputs the architecture to a JSON file:npx depcruise src --output-type json > ./.architecture/seams.json.
Step 2: Establish Temporal Coupling (Hercules / Git Mining)
Agent Instructions:
- Set up a tool to extract Logical Coupling from the git history.
(Note for Agent: Use the
src-d/herculesbinary via Docker or Go, OR use the simplercode-maatPython/Clojure alternative if Hercules is blocked by local environment constraints). - Execute a git log command to get history:
git log --all --numstat --date=short --pretty=format:'--%h--%ad--%aN' --no-renames > ./.architecture/logfile.log - Run the coupling analysis tool against this log. The goal is to output a
.csvor.jsonfile (coupling.json) with three columns/fields:[FileA, FileB, CoChangePercentage]. - Ensure this output is saved to
/.architecture/coupling.json.
Step 3: Implement Deep Static Analysis (Opengrep / Semgrep)
Agent Instructions:
- Install the open-source CLI (e.g.,
npm install -g opengrepor usepip install semgrep). - Create a folder
/.architecture/rules/. - Write custom YAML rules to detect code smells specific to Effect.ts and functional pipelines.
- Rule 1: Tramp Data in Pipelines. Write a rule that looks for a variable passed into a
.pipe()orEffect.genthat traverses across a recognized boundary un-mutated. Use the ellipsis operator (...). Example concept:rules: - id: effect-tramp-data pattern: pipe(..., $MOD_A.get($DATA), ..., $MOD_B.save($DATA), ...) message: "Potential tramp data crossing seam boundaries." languages: [typescript] severity: WARNING - Ensure there is a command to run this and output to JSON:
opengrep --config ./.architecture/rules/ --json > ./.architecture/smells.json
Step 4: Write the Synthesis Script (refactor-bot.ts)
Agent Instructions:
Write a Node.js TypeScript file (scripts/refactor-bot.ts) that acts as the brain. This script must:
1. Load the Data:
- Parse
seams.json(Which files belong to which boundary). - Parse
coupling.json(How often files change together). - Parse
smells.json(Where the AST anti-patterns are).
2. Detect Cross-Seam Leaks (High External Coupling):
- Look at pairs in
coupling.jsonthat co-change > 60% of the time, but live in different Seams according toseams.json. - Action: Flag these as Architectural Leaks. Check if
smells.jsonfound tramp data in these specific files.
3. Detect Splittable Contexts (Low Internal Cohesion):
- Look at files that exist in the same Seam (e.g., both are in
src/payments/), but have a co-change frequency of < 5%. - Action: Flag this boundary as a candidate to be split into two smaller bounded contexts.
4. Generate the Output/Report:
- Output a Markdown report summarizing:
- ⚠️ High Priority Refactors: Cross-layer tightly coupled files (includes Semgrep AST context).
- ✂️ Suggested Seam Splits: Modules with low internal cohesion.
- Exit with code
1if the max coupling threshold is breached to fail the CI/CD pipeline, otherwise exit with0.
Definition of Done for Agent:
dependency-cruiseris configured and outputsseams.json.- Git history pipeline outputs
coupling.json. - Opengrep/Semgrep YAML rules are written for Effect.ts and output
smells.json. refactor-bot.tssuccessfully reads all three files and prints an Evolutionary Architecture Report in the console.