Chronos/DBWaitlist

Blog / AI agents

How do you stop two AI agents breaking a rule together?

Two changes can each be fine alone and wrong together. A merge check is a query that must find nothing in the merged result, run inside every merge before anything is written.

Abhishek DG · 4 min read
  • AI agents
  • Merge checks
  • Correctness

Check the result, not each change. Two agents can each make a change that is fine alone and break a rule together, and no review of either one would show it. A merge check is a query that must find nothing in what a merge would produce. It runs inside every merge, on the merged result, before anything is written.

What does “fine alone, wrong together” look like?

Say every lead list must keep at least one lead. One agent moves the last-but-one lead out of a list. Another empties the same list’s remaining lead. Each diff looks reasonable. Together they leave an empty list.

  • Agent Amoves lead 7 from list 3 to list 5list 3 still has lead 9: fine
  • Agent Bdeletes lead 9, a duplicatelist 3 still has lead 7 in B’s copy: fine
  • A mergeslist 3 now holds only lead 9merged
  • B mergeslist 3 would hold nothingrefused: the check finds list 3
Each change passes on its own. Only the second merge’s result breaks the rule, so that is where it has to be checked.

How do I write the rule?

As a query that returns the rows that are wrong. If it returns nothing, the merge may go on.

SQL
create merge check lists_keep_leads on tables (leads, lists) as  select id from lists where id not in (select list from leads);merge world cleanup;-- ERROR:  merging cleanup breaks merge check lists_keep_leads, which finds (id = 3); nothing was merged

Return a column that names the wrong rows, such as an id: those rows become the reason in the error, and in the review queue.

Where does the check run?

Inside the merge, under its locks, on the world being merged into as the merge would leave it. That is the whole point: what is checked is exactly what would be written. With on tables, only merges that change those tables run the check; without it, every merge does.

How is this different from a policy or a constraint?

JudgesGood for
Merge policythe change: how many rows, which tables, any deletes“No delete lands without a person”
Merge checkthe data after the merge“Every list keeps a lead”, “no account below zero”, “every order’s lines add up to its total”
Constraintevery write, merge or notrules that must hold for your app’s own writes too

A check does not see direct writes. A transaction’s commit, or your app writing main, is not a merge. Use a constraint for a rule every write must keep.

Can anyone skip a check?

No. A person’s merge is refused the same as an agent’s. A person gets a held world through by changing it until it passes; an exception is made by changing the check itself, which is a visible change to the database. For an agent kept to a merge policy, a broken check is one more broken rule: its world waits in the review queue with the rows the check found.

What should a check not do?

  • Change anything. It is one select, and it may not call functions that write.
  • Run long. It runs inside every merge that touches its tables, holding that merge’s locks, and a merge it does not finish in is refused. The timeout is 1 second unless you set one (with (timeout = '2s')). Index the columns it filters on.
  • Replace review. A check catches the rules you thought of. The diff is still where you find the ones you did not.

More: Merge checks, and Merge policies and reviews for the rules about the change itself.

Questions

How can two AI agents break a rule when each change looks fine?

When the rule is about the data as a whole. One agent moves the second-to-last lead out of a list and another deletes the last one; each change keeps the list non-empty in its own copy, and together they empty it. Only the merged result shows the problem.

What is a merge check?

A query that must return no rows on what a merge would produce. Chronos runs it inside the merge, under its locks, before anything is written. If it finds rows, the merge is refused and those rows are given as the reason.

What is the difference between a merge check and a database constraint?

A constraint is checked on every write. A merge check is checked only when a branch is merged, on the merged result, and can express rules across tables as an ordinary query. Use a constraint for rules your application’s own writes must keep too.

Written by

Abhishek DG

Co-founder & CEO, 2090 Labs

Abhishek builds Chronos in Bangalore: the database where AI agents work in their own copy of your data. Before it he built TailsMart and ACRED India.

More about Abhishek

Keep reading

How do you stop two AI agents breaking a rule together? · Chronos