enforce-contract

Enforce Design by Contract with pre-conditions, post-conditions, and invariants.

2|Updated Dec 24, 2025
One-click install
npx skills add https://github.com/knowlet/skills --skill enforce-contract
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: enforce-contract
Source: https://github.com/knowlet/skills/tree/main/skills/enforce-contract
Command: npx skills add https://github.com/knowlet/skills --skill enforce-contract

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill codifies Design by Contract to prevent AI hallucinations by defining pre-conditions, post-conditions, and invariants, and by validating code with contract-driven tests.

Core Features & Use Cases

  • Pre-conditions: validate inputs before execution.
  • Post-conditions & Invariants: ensure post-state correctness and invariant maintenance.
  • Contract-driven testing: generate/verify tests from contracts.

Quick Start

When implementing a new Use Case or method, invoke enforce-contract to define and validate pre/post conditions and invariants, then run contract checks during tests and commits.

Frequently Asked Questions about enforce-contract

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I enforce pre-conditions and post-conditions in my code?

Enforce pre-conditions and post-conditions by defining explicit contract annotations in Javadoc style, then validating inputs before execution and verifying post-state correctness after method completion. This prevents invalid states and catches logic errors early.

What is Design by Contract and how does it prevent AI hallucinations?

Design by Contract codifies expected behavior through pre-conditions, post-conditions, and invariants that methods must satisfy. It prevents AI hallucinations by forcing explicit, testable specifications before implementation rather than relying on implicit assumptions.

How do I write contract-driven tests for my methods?

Write contract-driven tests by generating test cases directly from contract specifications, validating that pre-conditions gate entry, post-conditions hold on exit, and invariants remain true throughout execution. Automate checks for nulls, empty collections, positive numbers, and valid state transitions.

When should I apply Design by Contract to new methods?

Apply Design by Contract when implementing new methods, drafting unit tests, or verifying AI-generated code. It's essential before pre-commit submission to ensure method contracts are explicit and machine-verifiable.

Can I use Design by Contract with existing unit tests?

Yes. Overlay contract specifications onto existing unit tests by extracting implicit assumptions as explicit pre-conditions, post-conditions, and invariants, then validating them through automated checks during test execution and builds.

What invariants should I enforce in stateful code?

Enforce invariants that must remain true across all valid state transitions—such as collection non-emptiness, numeric bounds, or relationship consistency. Automated checks verify invariants hold before and after each method call.