Skip to main content
You will give each contract a review status made from its clauses’ answers, and your application will get an event when a contract starts or stops needing a review. Brussle judges the contract again after you edit a clause only if what the contract reads of that clause changed. The judgment one_sided asks each clause “Does this clause favour the other party over the customer?”, and the judgment contract_terms reads those answers to decide whether the contract needs a review. A judgment that reads the answers of other judgments is a roll-up. The number of flagged clauses becomes a feature, which the fit on your outcomes for contract_terms weighs. Without a roll-up, you would build a pipeline that copies each clause’s answer into the contract and writes the contract again whenever an answer changes. A roll-up puts the right answers beside the question. It does not make the engine more accurate. Read judge a document with its related documents first. The rules of roll-ups, and what a change to a judgment costs the judgments that read it, are on when other judgments read this one.

Define the clause judgment

The example keeps two kinds of document in one namespace:
  • A contract has attributes.kind contract.
  • A clause has attributes.kind clause, and its attributes.contract_id holds the id of its contract.
Create both judgments before you write the documents, since a document written before its judgment existed waits for a backfill. The examples use ns = db.namespace("acme/prod"), which needs a key whose scope covers acme/prod (such as acme/*), since the key you got at sign-up cannot reach it, and only an organization with a card on file can create one that does (get an API key). one_sided is a plain bool judgment that reads only the clause, and its named threshold flag is true when p is 0.8 or more. Its policy must be on_change, so that its answers are current when the contract reads them (see what a judgment can read).

Define the contract judgment

A relation reads the current answer of a judgment at answers.<judgment>.<field>, the same paths a query filters on (see the answer paths). contract_terms reads answers.one_sided.p of each contract’s clauses as a band, and its threshold review (true when p is 0.7 or more) is the contract’s review status. A judgment with a relation needs confirm: true; without it, Brussle creates nothing and returns an estimate of the monthly cost.
Create it with ns.judgments.create(**contract_terms) in Python, or ns.judgments.create(contractTerms) in TypeScript. Two freshness settings serve this walk-through:
  • debounce_ms is 2 seconds, so that you see each change at once (with features, the default is 10 minutes). See chatty documents.
  • wait_for_children makes the contract wait for its clauses’ answers (see wait for the clauses).
The context shows each clause’s one_sided answer as a word beside the clause’s heading, and a clause with no answer yet shows no answer field.
The aggregate has "render": false, so the counts stay out of the context. Brussle keeps them beside each answer as features:
  • clauses.count: the clauses that the relation read.
  • clauses.count_where: those clauses whose flag threshold on one_sided is true (a clause with no answer does not count).
The feature threshold many_one_sided reads count_where directly, with no engine, and is true when three or more clauses are flagged, so a query or a subscription can filter on it. Keep a number such as this as a feature instead of text for the engine to count, since the fit on your outcomes weighs it (see numbers from relations) and the report shows whether it helps.

Write a contract and its clauses

wait_for makes the write wait, for at most wait_timeout_ms, until each clause has its one_sided answer. It waits only for the documents that one_sided judges, so it does not wait for the contract.
The four documents are durable when the write returns. The first answers of a new judgment can take longer than the wait, and a clause whose answer is not ready comes back pending.

Read the contract’s first answer

Read the contract until its answer is fresh. The helper stops after 60 seconds or when judging failed, and given an earlier answer it also waits for a newer evaluation (the next step uses this).
Brussle judges the contract once, after the three one_sided answers land, because the judgment has wait_for_children. The contract’s answer records what the roll-up read, beside the fields of every answer:
  • freshness is fresh when the answer agrees with the contract, each clause answer it read, and the active version.
  • p is the engine’s probability until a fit with features applies, and from then on it is the corrected number, with fit naming the fit.
  • thresholds holds review and many_one_sided, read by the current thresholds against this answer.
  • features holds clauses.count (3 here) and clauses.count_where, as this evaluation read them.
  • watermark says how far into the namespace’s writes Brussle had read when it read the clauses, and answers_generation how far it had read the answers of one_sided. A newer clause answer that changes what the contract shows makes the answer pending.
  • evaluation_id names the evaluation, the permanent record of this judging.

Wait for the clauses

wait_for_children makes Brussle hold the contract while a clause it reads has a one_sided answer that reads pending, then judge it once, so a load of clauses costs one evaluation of the contract instead of two. On an existing judgment it is a setting that needs no new version, so you can turn it on with ns.judgments.update("contract_terms", freshness={"wait_for_children": True}).
  • Brussle does not wait for an answer that reads stale (limit_reached or judging_paused), failed or unavailable, because no new answer is on its way.
  • Brussle waits at most max_wait_ms after the oldest change it has not judged (10 minutes when no ceiling is in effect), then judges the contract with the answers it has.
A clause that is behind gives the contract its last answer, and the contract’s evaluation names the judgment under upstream:
  • child_pending: the clause has a write not yet judged, or its new judging waits for a rolling limit, a budget pause or a retry. You need to do nothing, since Brussle judges the contract again if the new answer changes what the contract shows.
  • child_out_of_scope: no new answer is coming, because the clause failed for its own reason or has no answer. Write the clause again, or backfill one_sided.
These entries describe what that evaluation saw, not the current state of the clauses. See loads of related documents.

Edit a clause and watch the contract follow

Edit the payment clause by writing the clause (not the contract), then wait for a newer answer of the contract.
  1. Brussle judges the clause again, and its one_sided answer moves. The new text usually crosses into the one-sided band, and flag becomes true.
  2. The new answer changes what the contract’s relation shows, so the contract’s answer becomes pending.
  3. Brussle judges the contract again after the contract’s debounce_ms, so the engine sees the new band and clauses.count_where goes up by one.
The engine decides where the clause’s new p lands. If p stays inside its old band and under the flag cut, the contract reads nothing new, so its answer stays fresh with the same evaluation_id and the helper returns it at its deadline. Brussle does not judge the contract again for a change the contract cannot see. wait_for on the clause write cannot wait for the contract, since it waits only for the documents in the write.

When the contract is judged again

A new answer of a clause makes Brussle judge the contract again only when it changes what a relation of the contract shows:
  • its band
  • whether it agrees with a cut or a threshold in count_where or match
  • the band that it gives to a min or a max
  • its latest value
A thousand answers of a clause whose p stays inside one band cost the contract nothing. A change that counts is an ordinary change to a related document, so the contract’s debounce_ms and ceiling apply. A burst of clause answers costs one evaluation of the contract, and answers that never stop changing still judge it once max_wait_ms has passed after the oldest change it did not read. These re-judges count against the contract judgment’s rolling limit (300,000 in any 30 days by default), whether or not they need an engine call. Past the limit, the contracts that do not fit read stale (limit_reached) instead of pending until the window has room. That is how a contract that reads no referenced document can still read stale. A clause that you delete and write again with the same id is a new clause, so its first answer makes Brussle judge the contract again, even when the number is the same as before.

Find the contracts that need a review

A contract needs a review when its review threshold is true. Query those contracts with answers: "fresh_only", so that Brussle leaves out a contract whose answer is pending after a clause change until its new answer lands. fresh_only applies to the answers in include, so include contract_terms.
To filter on the count instead, use the feature threshold (["answers.contract_terms.thresholds.many_one_sided", "Eq", true]) or the feature itself, such as ["answers.contract_terms.features.clauses.count_where", "Gte", 2]. A subscription saves the same filter and sends subscription.entered when a contract starts to need a review, and subscription.exited when it stops, to a webhook endpoint and to the events feed.
  • The subscription starts syncing while its first check records the contracts that match now, with no events, and then it is live.
  • It sees only settled answers, so it sends nothing while the contract’s answer is pending, then one event if the new answer changes the match.
  • An event carries the attributes and answers that the filter reads or include names, and never state (see event fields).
  • A threshold change on one_sided, or a new fit on contract_terms, changes many contracts at once, so by default the subscription sends one subscription.synced for such a bulk change instead of an event for each contract.
ns.subscriptions.query("contracts-to-review") runs the saved filter as a query. Use it to fill a receiver, and after each subscription.synced.

Inspect an unexpected answer

When a contract’s answer looks wrong, open its evaluation before you change the question (ns.evaluation(id) in both SDKs, GET /namespaces/{ns}/evaluations/{id} in the API), since the evaluation records exactly what the engine read. A fresh answer agrees with each document it read, although the engine may not have read every clause or the newest answer of each. See a fresh answer can have incomplete inputs.
Read these fields, in this order:
  • related_documents: each clause that the engine read, with its revision and the one_sided evaluation behind its answer (see the audit). A missing clause was not in the relation’s selection, so check its attributes.contract_id and the relation’s match.
  • upstream: present when a clause was behind and the contract read its last answer (see wait for the clauses).
  • context_truncated: true when the context passed max_tokens or the engine’s limit and Brussle cut part of it, so the engine did not see every selected clause. relations.clauses.dropped counts the records left out. Show fewer fields of each clause, cut long fields with max_chars, or lower last_n.
  • relations_capped: true when the relation stopped before it checked every matching document, so the counts and the context may be missing a clause. relations.clauses shows capped, range_full or walk_exhausted. Narrow the relation’s match on attributes.
  • relations.clauses: the clauses the relation scanned, selected and rendered, and their tokens. If selected is lower than you expect, check last_n and window.
  • answers_generation and watermark: how far this evaluation had read the clauses’ answers and the documents. Compare them with the contract’s current answer to see whether a newer clause answer has arrived since.
When the inputs are complete and the answer still looks wrong, change the question. That makes a new version, so compare it on your outcomes in the report before you activate it.

The audit

Each entry of related_documents names, in evaluations, the evaluation behind each answer that the contract read (such as {"one_sided": "ev_…"}). Open that evaluation to see the clause text that the engine read.
  • When the clause judgment has a fit with features, the entry also names in fits the fit under which Brussle read its p.
  • When the clause judgment reads answers itself, its evaluation names the answers it read, so you can follow an answer down through each level by id.
The audit names what each evaluation read, which can differ from what is newest now. Brussle keeps evaluations until you delete the namespace.