Skip to main content
Rules are codified standards that Droid follows every time. Unlike memories (which capture decisions and context), rules define how code should be written. This guide shows you how to organize rules effectively for individuals and teams.
Works with Factory App: These conventions work identically in both the Droid CLI and the Factory App—Droid reads the same .factory/rules/ files regardless of interface.

Rules vs Other Configuration

Rules are prescriptive—they tell Droid what to do. Use them for standards that should apply consistently.

Setting Up a Rules Directory

Basic Structure

Create a .factory/rules/ directory in your project:
For personal rules that apply across all projects:

Referencing Rules in AGENTS.md

Add a section to your AGENTS.md:

Writing Effective Rules

Rule Structure

Each rule should be:
  • Specific: Clear enough to follow without interpretation
  • Actionable: Tells what to do, not just what to avoid
  • Scoped: States when it applies
  • Justified (optional): Explains why for complex rules

Template


Example Rules Files

TypeScript Rules

Create .factory/rules/typescript.md:

Avoid any

Applies to: All TypeScript files Rule: Never use any. Use unknown with type guards, or define proper types.

Function Patterns

Use early returns

Applies to: All functions with conditionals Rule: Return early for edge cases instead of nesting.

Named exports over default

Applies to: All module exports Rule: Use named exports for better refactoring and import clarity.

Component file structure

Applies to: All component files Rule: Order sections as: imports, types, component, exports.

State Management

Zustand for client state

Applies to: Client-side state that isn’t server data Rule: Use Zustand stores in src/stores/. One store per domain.

React Query for server state

Applies to: All data fetched from APIs Rule: Use React Query. Queries go in src/queries/.
src/ └── components/ └── UserCard/ ├── UserCard.tsx ├── UserCard.test.tsx # ✅ Colocated └── index.ts

One assertion per test

Applies to: Unit tests Rule: Test one behavior per test case. Multiple assertions OK if testing same behavior.

Mocking

Mock at boundaries

Applies to: All mocked dependencies Rule: Mock external APIs and services, not internal functions.

Use MSW for API mocking in integration tests

Applies to: Integration tests that need API responses Rule: Use Mock Service Worker instead of mocking fetch directly.

Validate environment variables

Applies to: Application startup Rule: Validate required env vars exist at startup.

Input Validation

Validate all external input

Applies to: API routes, form handlers Rule: Use Zod to validate all input from users or external sources.

Error Handling

Never expose internal errors

Applies to: API error responses Rule: Log detailed errors server-side; return generic messages to clients.

Authentication

Check authentication on every protected route

Applies to: All API routes requiring auth Rule: Use middleware or guards. Never assume auth from client.
.factory/rules/ ├── _base/ # Foundation rules (everyone follows) │ ├── typescript.md │ └── security.md ├── frontend/ # Frontend-specific │ ├── react.md │ └── styling.md ├── backend/ # Backend-specific │ ├── api.md │ └── database.md └── testing/ # Testing standards ├── unit.md └── integration.md

Rule Ownership

Add ownership to track who maintains each rule set:

Current Limitation: No Glob Pattern Support

Currently, Droid doesn’t support conditional rule application based on file patterns (e.g., “apply these rules only to *.tsx files”). This is on the roadmap.
Workarounds:
  1. Organize by file type: Create separate rules files and reference them contextually
  1. Use clear scoping in rules: State applicability clearly
  1. Use skills for complex workflows: Skills can encode file-type-specific instructions

Rules Maintenance

Adding New Rules

When you find yourself correcting Droid repeatedly:
  1. Identify the pattern
  2. Write a clear rule with examples
  3. Add to appropriate rules file
  4. Update AGENTS.md if needed
  5. Test by asking Droid to do similar work

Reviewing Rules

Quarterly review checklist:
  • Remove rules that are now enforced by linting
  • Update rules that have changed
  • Add rules for new patterns
  • Check that examples are still accurate
  • Verify AGENTS.md references are current

Deprecating Rules

When a rule becomes outdated:

Enforcing Rules Automatically

While Droid follows rules from your .factory/rules/ files, you can add automated enforcement with hooks.

Run Linters After Edits

Add a PostToolUse hook to run your linter after every file edit:

Validate Code Style

Run Prettier or your formatter automatically:
See Hooks reference for more examples.

Quick Reference

File Locations

Rule Format

Good Rules Are

  • ✅ Specific and unambiguous
  • ✅ Include code examples
  • ✅ State when they apply
  • ✅ Actionable (do X, not “consider X”)
  • ❌ Not vague (“write clean code”)
  • ❌ Not duplicating linter rules
  • ❌ Not contradicting other rules

Next Steps

Setup Checklist

Complete power user configuration

Memory Management

Capture decisions and context