Back to .md Directory

ContractSpec Codebase

Defines a spec-first compiler approach for AI-generated code, with rules, commands, and governance for multi-surface consistency.

May 2, 2026
0 downloads
2 views
ai agent safety
View source

What this file does

Defines a spec-first compiler approach for AI-generated code, with rules, commands, and governance for multi-surface consistency.

When to use it

  • Adopting a contract-first workflow for AI-assisted development
  • Setting up governance and traceability for AI code generation
  • Standardizing code style and file size limits across a TypeScript project
  • Establishing a regeneration-safe codebase with spec-driven consistency

Assumes this stack

TypeScriptZodReplit

ContractSpec Codebase

Mission: ContractSpec is the deterministic, spec-first compiler that keeps AI-written software coherent, safe, and regenerable.

Quick Reference

ConcernRule FileKey Points
Contractscontracts-first.mdSpec before implementation
Mission & Contextcontractspec-mission.mdWhy we exist, who we serve
Architecturepackage-architecture.mdlibs → bundles → apps
Code Qualitycode-quality-practices.mdTesting, naming, standards
File Organizationcode-splitting.mdMax 250 lines, domain grouping
Type Safetytype-safety-dependencies.mdNo any, latest deps
Frontendfrontend.mdAtomic design, state handling
Backendbackend.mdHexagonal, DDD
Design Systemdesign-system-usage.mdNo raw HTML
AI Governanceai-agent.mdTraceability, composability
Documentationdocs.mdDocBlocks first

Core Principles

  1. Spec-First: Contracts define behavior before implementation
  2. Multi-Surface Consistency: One spec → API, DB, UI, events
  3. Safe Regeneration: Code can be regenerated without breaking invariants
  4. Standard Tech: TypeScript, Zod, no magic abstractions
  5. Incremental Adoption: Stabilize one module at a time

Code Style (Enforced)

  • Language: TypeScript (strict mode)
  • Formatting: 2 spaces, semicolons, double quotes, trailing commas
  • Types: Explicit, no any, proper type guards
  • Files: Max 250 lines (components: 150, utilities: 100)

Before You Code

  1. Read relevant rules for your change type
  2. Check existing patterns in the codebase
  3. Plan with DocBlocks if adding new features
  4. Run /ai-audit to verify decisions

AI Agent Guidelines

When working with AI assistants:

  • Rules are applied contextually based on file type
  • Conflicts resolved by: Security > Compliance > Safety > Quality > UX > Performance
  • All decisions should be traceable and reversible
  • Use /ai-audit to verify governance compliance

Commands Available

  • /commit - Create conventional commit
  • /test - Run tests with analysis
  • /lint - Check and fix lint issues
  • /fix - Auto-fix common issues
  • /explain - Understand code
  • /refactor - Guided refactoring
  • /review-pr - Code review
  • /ai-audit - Governance check

Getting Help

  • Check the specific rule file for detailed guidance
  • Use /explain <concept> for architecture questions
  • Run /ai-audit to verify your approach

What's inside

One mission statement, 5 core principles, 4 code style rules, 3 pre-coding steps, 8 commands, and a table of 11 rule files.

Change this for your project

  • Replace lssm-tech/contractspec with your own repository name
  • Replace /ai-audit with your own governance command if different
  • Replace ContractSpec with your project's compiler or tool name

Where it goes

Keep it in your repository where the agent or team that needs it will read it.

Worth borrowing

  • Spec-first contracts that define behavior before implementation
  • Conflict resolution hierarchy: Security > Compliance > Safety > Quality > UX > Performance
  • Max line limits per file type (250 general, 150 components, 100 utilities)

Related Documents