Code Structure Cleanup
Run this after the feature works. Do not use it as permission to redesign Zoe.
The goal is to reduce repeated mechanics so future agents can understand and repair the code safely.
When To Use
- A feature works, but similar helper logic appears in multiple files.
- Provider calls, parsing, validation, command execution, or payload transforms are repeated.
- A bug fix in one path would not reach another path doing the same operation.
- Greptile or a human review calls out duplication that affects correctness or maintainability.
Do not extract logic that is used once or is clearly domain-specific.
Zoe Boundary Rule
Routes, intents, actions, and UI handlers own product policy:
- user and scope checks,
- auth and RBAC decisions,
- status transitions,
- proposal/approval flow,
- user-facing wording,
- AG-UI response shape.
Service-layer functions own reusable mechanics:
- provider or SDK calls,
- request/response normalization,
- validation that is not domain policy,
- command execution details,
- retry/readiness mechanics,
- payload transforms.
Design service functions as small capability blocks:
- accept required data as explicit parameters,
- return structured outputs instead of loose strings or hidden side effects,
- avoid reaching into database state unless the service owns that persistence boundary,
- make failure explicit with structured results or clear exceptions,
- keep API shapes consistent across related helpers.
Process
- Inspect only the files touched by the feature and their immediate callers.
- Name the duplicated mechanics clearly.
- Extract the smallest repeated non-domain block into an existing service module when one fits.
- Replace one caller, verify behavior, then replace the remaining callers.
- Keep names and call shapes close to existing Zoe style.
- Run focused tests and Zoe validators.
Guardrails
- Do not create duplicate routers or files ending in
_new, _fixed, _v2, _old, or _backup.
- Do not move domain policy into shared services.
- Do not create a god service that hides all control flow.
- Do not create leaky services that mutate unrelated domain state or depend on hidden globals.
- Do not extract one-off logic only to make the code look more abstract.
- Do not touch retired production code under
services/zoe-core/ except as archive context.
- Do not change user-facing behavior unless the user explicitly asked for that change.
Verification
For Zoe repo work, start with:
cd /home/zoe/assistant
python3 tools/audit/validate_structure.py
python3 tools/audit/validate_critical_files.py
Then run focused tests for the touched area. For live user-facing changes, also check:
curl -sf http://127.0.0.1:8000/health
curl -sf http://127.0.0.1:8000/api/system/status
1---2name: code-structure-cleanup3description: Use after a Zoe feature works but duplicated runtime mechanics, repeated provider calls, parsing, validation, or command execution should be moved into small reusable service-layer functions without changing behavior.4license: MIT5---67# Code Structure Cleanup89Run this after the feature works. Do not use it as permission to redesign Zoe.1011The goal is to reduce repeated mechanics so future agents can understand and repair the code safely.1213## When To Use1415- A feature works, but similar helper logic appears in multiple files.16- Provider calls, parsing, validation, command execution, or payload transforms are repeated.17- A bug fix in one path would not reach another path doing the same operation.18- Greptile or a human review calls out duplication that affects correctness or maintainability.1920Do not extract logic that is used once or is clearly domain-specific.2122## Zoe Boundary Rule2324Routes, intents, actions, and UI handlers own product policy:2526- user and scope checks,27- auth and RBAC decisions,28- status transitions,29- proposal/approval flow,30- user-facing wording,31- AG-UI response shape.3233Service-layer functions own reusable mechanics:3435- provider or SDK calls,36- request/response normalization,37- validation that is not domain policy,38- command execution details,39- retry/readiness mechanics,40- payload transforms.4142Design service functions as small capability blocks:4344- accept required data as explicit parameters,45- return structured outputs instead of loose strings or hidden side effects,46- avoid reaching into database state unless the service owns that persistence boundary,47- make failure explicit with structured results or clear exceptions,48- keep API shapes consistent across related helpers.4950## Process51521. Inspect only the files touched by the feature and their immediate callers.532. Name the duplicated mechanics clearly.543. Extract the smallest repeated non-domain block into an existing service module when one fits.554. Replace one caller, verify behavior, then replace the remaining callers.565. Keep names and call shapes close to existing Zoe style.576. Run focused tests and Zoe validators.5859## Guardrails6061- Do not create duplicate routers or files ending in `_new`, `_fixed`, `_v2`, `_old`, or `_backup`.62- Do not move domain policy into shared services.63- Do not create a god service that hides all control flow.64- Do not create leaky services that mutate unrelated domain state or depend on hidden globals.65- Do not extract one-off logic only to make the code look more abstract.66- Do not touch retired production code under `services/zoe-core/` except as archive context.67- Do not change user-facing behavior unless the user explicitly asked for that change.6869## Verification7071For Zoe repo work, start with:7273```bash74cd /home/zoe/assistant75python3 tools/audit/validate_structure.py76python3 tools/audit/validate_critical_files.py77```7879Then run focused tests for the touched area. For live user-facing changes, also check:8081```bash82curl -sf http://127.0.0.1:8000/health83curl -sf http://127.0.0.1:8000/api/system/status84```