Loading...
Loading...
Repository structure methodology for maximum AI agent effectiveness. Three pillars — context engineering (repo as knowledge product), architectural constraints (deterministic enforcement), garbage collection (active entropy fighting). Use when setting up repos for AI development, diagnosing repeated agent failures, writing AGENTS.md, or designing CI gates and structural tests.
npx skill4agent add majesticlabs-dev/majestic-marketplace harness-engineering| Pillar | What It Solves |
|---|---|
| Context Engineering | Agent hallucinations, wrong tool usage, stale docs |
| Architectural Constraints | Boundary violations, silent regressions, ambiguous failures |
| Garbage Collection | Entropy accumulation, dead code, doc drift |
| Situation | Action |
|---|---|
| New repo setup for AI development | Apply all three pillars from day one |
| Agent repeatedly hallucinating tools/APIs | Pillar 1: add tool declarations to AGENTS.md |
| Agent crossing module boundaries | Pillar 2: add structural test enforcing boundary |
| Agent producing code that passes CI but breaks conventions | Pillar 2: convert convention to linter rule or structural test |
| Docs drifting from implementation | Pillar 1: add CI cross-link validation |
| Codebase growing, agent quality degrading | Pillar 3: schedule GC agent for dead code and unused exports |
| Agent ignoring AGENTS.md guidance | Check: is guidance generic advice or a specific failure lesson? Rewrite as failure ledger entry |
| Post-incident on agent-generated code | Add failure to AGENTS.md, add constraint to prevent recurrence |
# Pattern
BAD: "Follow clean code principles"
BAD: "Use meaningful variable names"
GOOD: "Never import from packages/internal — agent imported shared/db directly on 2025-12-03, broke build"
GOOD: "Always use OrderService.create(), not Order.new — direct instantiation skips validation (incident #247)"rule: Never call PaymentGateway directly from controllers
context: Agent bypassed service layer, sent duplicate charges (2025-11-15)
fix: Use PaymentService.charge() which handles idempotency## Available Tools
- `bin/test` — Run test suite (prefer over raw pytest/rspec)
- `bin/lint` — Run linters with auto-fix
- `bin/db-reset` — Reset dev database
- `make deploy-staging` — Deploy to staging (requires approval)
## DO NOT USE
- `rm -rf` on any directory
- Direct database queries in production
- `curl` to external APIs without going through ApiClientRULE: docs/ is canonical truth
ENFORCEMENT: CI validates cross-links between docs/ and source code
MECHANISM:
- Every public API must have a corresponding docs/ entry
- CI script checks: for each @api-doc tag in source → matching file in docs/
- Broken link = CI failure, not warningPATTERN:
- Assign task_id to each agent invocation
- Route logs to task-specific output (file, log group, trace)
- Post-task: review task log for failures, add to AGENTS.md if new pattern# BAD linter output
ERROR: Import violation in src/api/handler.ts
# GOOD linter output
ERROR: Import violation in src/api/handler.ts
↳ Cannot import from 'packages/db' in 'src/api/'
↳ Use 'packages/db-client' instead (facade pattern)
↳ See: docs/architecture/data-access.md# Pseudocode structural tests
test "no cross-boundary imports":
For each FILE in src/api/**:
IMPORTS = parse_imports(FILE)
FORBIDDEN = ["packages/internal", "src/admin", "src/worker"]
assert IMPORTS intersection FORBIDDEN == empty
test "dependency direction":
LAYERS = [presentation, application, domain, infrastructure]
For each LAYER in LAYERS:
For each IMPORT in LAYER.imports:
assert IMPORT.layer_index >= LAYER.index # only import same or lower
test "API contract stability":
CURRENT = parse_openapi("api/openapi.yml")
PREVIOUS = parse_openapi("api/openapi.yml", ref="main")
assert no_breaking_changes(CURRENT, PREVIOUS)| Check Type | Gate Implementation |
|---|---|
| Test coverage | |
| Bundle size | |
| Lint errors | |
| Type errors | |
| Security vulns | |
# .dependency-layers.yml (or equivalent config)
layers:
- name: presentation
paths: ["src/ui/**", "src/api/routes/**"]
can_import: [application, domain]
- name: application
paths: ["src/services/**", "src/use-cases/**"]
can_import: [domain, infrastructure]
- name: domain
paths: ["src/models/**", "src/entities/**"]
can_import: [] # domain has no dependencies
- name: infrastructure
paths: ["src/db/**", "src/external/**"]
can_import: [domain]GC_TASKS:
- dead_code: find unused exports, unreachable functions → remove
- stale_docs: find docs/ entries with no matching source → flag
- unused_deps: find package.json/Gemfile entries with no imports → remove
- orphan_tests: find test files with no matching source file → flag
- config_drift: diff .env.example vs actual config usage → reconcile
FREQUENCY: weekly for active repos, monthly for stable repos
OUTPUT: one small PR per GC task (not one mega-PR)
MERGE: auto-merge if CI passes, otherwise flag for reviewPRINCIPLE: fast feedback > comprehensive analysis
Examples:
- bin/check-api-contracts → validates OpenAPI spec matches routes (5s)
- bin/check-imports → validates dependency layers (2s)
- bin/check-docs → validates doc cross-links (3s)
RULE: if a custom check takes > 30s, it's too slow for agent feedback loops1. Check AGENTS.md — is the constraint documented?
If NO → add it (Pillar 1 fix)
If YES → is it enforced in CI?
2. Check CI — does a gate catch this failure?
If NO → add structural test or linter rule (Pillar 2 fix)
If YES → is the error message teaching?
3. Check error message — does it include remediation?
If NO → improve the error message (Pillar 2.1 fix)
If YES → the harness is correct, investigate agent-specific issue
NEVER: Skip to "the agent is broken" without completing steps 1-3docs/hierarchical-agentstdd-workflowstructured-loggingquality-gate