tpt-law
TypeScriptEncode legislation as executable YAML rules. Apply law to facts via REST API and get decisions with full reasoning traces — built for AI-assisted legal judgment. New Zealand (Working for Families) included.
Languages
tpt-law
Law as executable, auditable, AI-consumable infrastructure.
Open source (MIT). New Zealand as the first jurisdiction. Built to scale to any legal system.
Vision
Legal systems are slow, expensive, and inconsistent — not because the law is unclear, but because applying it requires enormous human effort for tasks that are fundamentally mechanical. tpt-law encodes legislation as formal, executable rules so that AI systems can apply the law correctly, consistently, and with a full audit trail.
The long-term goal is AI-assisted legal judgment: starting with decision-support tools, building toward systems that can autonomously apply law to facts in bounded domains.
Architecture
┌─────────────────────────────────────┐
│ AI / Applications (your code) │
└──────────────┬──────────────────────┘
│ REST API / MCP tool-use
┌──────────────▼──────────────────────┐
│ tpt-law engine (this repo) │
│ POST /apply · GET /rules │
└──────────────┬──────────────────────┘
│ loads
┌──────────────▼──────────────────────┐
│ Rule library (this repo) │
│ rules/nz/income-tax-act-2007/ │
│ rules/nz/social-security-act/ │
└─────────────────────────────────────┘
The rule library is the public good — open, auditable, correctable by anyone.
The engine is the infrastructure — jurisdiction-agnostic, runs any rule library.
AI is the application layer — queries the engine via standard HTTP or MCP.
Quickstart
cd engine
npm install
npm run dev
Apply Working for Families rules to a set of facts:
curl -X POST http://localhost:3000/apply \
-H "Content-Type: application/json" \
-d '{
"jurisdiction": "nz",
"legislation": "income-tax-act-2007",
"facts": {
"dependent_children": 2,
"nz_tax_resident": true,
"is_principal_caregiver": true,
"is_transitional_resident": false,
"annual_income": 60000,
"receiving_main_benefit": false,
"hours_worked_per_week": 40
},
"as_at": "2024-01-01"
}'
Response:
{
"jurisdiction": "nz",
"legislation": "income-tax-act-2007",
"as_at": "2024-01-01",
"satisfied_rules": ["wff_eligible", "wff_ftc_amount", "wff_iwtc_eligible", "wff_iwtc_amount"],
"decision": {
"wff_eligible": true,
"ftc_annual": 7384,
"iwtc_eligible": true,
"iwtc_annual": 3770
},
"trace": [
{
"rule": "wff_eligible",
"source": "Income Tax Act 2007 s.MD.1",
"status": "satisfied",
"conditions": [
{ "outcome": "matched", "fact": "dependent_children", "description": "dependent_children (2) >= 1" },
{ "outcome": "matched", "fact": "nz_tax_resident", "description": "nz_tax_resident = true" },
{ "outcome": "matched", "fact": "is_principal_caregiver", "description": "is_principal_caregiver = true" }
],
"result": { "wff_eligible": true }
}
],
"missing_facts": []
}
Rule Format
Rules are YAML files in rules/:jurisdiction/:act/. Each rule is immutable once published — new law versions get new files with an effective_from date.
rule: wff_eligible
source: "Income Tax Act 2007 s.MD.1"
effective_from: "2007-04-01"
given:
- fact: dependent_children
greater_than_or_equal: 1
- fact: nz_tax_resident
equals: true
- fact: is_principal_caregiver
equals: true
unless:
- fact: is_transitional_resident
equals: true
result:
wff_eligible: true
See docs/rule-format.md for the full specification.
Project Structure
tpt-law/
├── engine/ # TypeScript execution engine + REST API
│ └── src/
│ ├── loader/ # Rule file loading and validation
│ ├── engine/ # Rule evaluation logic
│ ├── api/ # Fastify HTTP routes
│ └── types/ # Shared TypeScript types
├── rules/
│ └── nz/ # New Zealand legislation (first jurisdiction)
├── schemas/ # JSON Schema for rule format validation
├── docs/ # Encoding guides, API reference
└── examples/ # Integration examples (Claude, OpenAI, MCP)
Principles
- Auditability first — every decision includes a full reasoning trace
- Temporal correctness —
as_atdate selects the right law version for any point in time - Open rule library — the encoded law is the public good; anyone can propose corrections
- Jurisdiction-agnostic engine — NZ is the first model, not a constraint
- AI-native API — designed for tool-use and MCP from the ground up
Contributing
See CONTRIBUTING.md for how to encode a new law or propose a correction to existing rules.
Status
Active development. Phases 1–10 complete. Phase 11 (Top 20 NZ Statutes) is at 25% coverage — 5 of 20 priority acts encoded. See TODO.md for the full roadmap.
License
MIT — see LICENSE.