Rules NO ONE can break
More and more code is written by coding agents, and soon most of it will be. Hive is no exception: I build it with one at my side. That changes what a project needs from its language: a way to state the few rules that must never be broken, and have the build refuse every change that breaks them, whoever or whatever wrote it. In Hive, that is a law.
Asking nicely isn't enough
Every project has rules that are not negotiable. A customer only ever sees their own data. A discount never takes a price below zero. A cancelled booking stays cancelled. Until now, those rules have lived in places that only ask: a comment, a code review, a test that tries a handful of cases, and lately an AGENTS.md that asks the robot, very politely and with lots of emotional manipulation (despite its emotionless nature), to please not do that.
None of those hold when nobody is watching. A reviewer skims the fortieth file of a pull request. Unit tests only catch the scenarios someone already thought of. An agent working through a long task reads the instruction, and three steps later writes the one query that leaks another customer's notes. Not out of malice: nothing stopped it.
A law is a rule the compiler proves for every possible value before it builds anything. If it cannot prove it, the program does not compile, and nothing lets a program accept a law it could not prove. The rule stops being something you hope everyone remembers and becomes something nobody can get past.
How: a rule on your data
Say every query on the notes table has to belong to one account. Write that once, as a law on the table:
type Account {
id: Int
}
type Note {
body: Str
}
law ownNotesOnly(account: Account) for table notes {
account_id = {account.id}
}
query search(account: Account, word: Str): Note[dyn] {
SELECT body FROM notes WHERE body LIKE {word}
}The search query above forgot the account. We've all done it. The build hasn't:
$ hive check notes.hive
notes.hive:13: in `search`: this query reads `notes` without the filter law `ownNotesOnly` keeps on it
its WHERE has to carry `account_id = {account.id}`The error names the rule and says what is missing, which is exactly what an agent needs to fix its own mistake. The rule does not depend on the agent having read the right file; it depends on the code compiling.
How: a rule on your types
A law written inside a type is kept by every value of that type, everywhere one is built or changed:
type Percent {
value: Int
law inRange(p: Percent) {
assert p.value >= 0 && p.value <= 100
}
}
func double(p: Percent): Percent {
return Percent(p.value * 2)
}$ hive check percent.hive
percent.hive:11: in `double`: cannot prove `Percent.inRange` for this `Percent(...)`: nothing here shows `2 * p.value <= 100`
known: 0 <= 2 * p.value; 0 <= p.value; p.value <= 100
guard it, so that the law holds hereIn return, no code that takes a Percent ever checks it again, and a decoder refuses a value that breaks the law as it reads it.
What it does not do
Can someone just delete the law? Sure. But that's a line in a diff that says, in plain words, "this rule no longer holds", and that's a lot harder to miss in review than a bug buried in the fortieth file.
There are limits, too. A law cannot follow a loop other than for each and a counting loop, or a function from an imported Go file, and it proves nothing about Float arithmetic. SQL built at run time with hive.sql.unsafe is not checked. Hive is pre-1.0, and laws are its newest part.
If your project has a rule you'd stake it on, write it down where the compiler can read it. The tour shows how: Laws and Laws on a table.