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.kindcontract. - A clause has
attributes.kindclause, and itsattributes.contract_idholds theidof its contract.
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 atanswers.<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.
ns.judgments.create(**contract_terms) in Python, or ns.judgments.create(contractTerms) in TypeScript.
Two freshness settings serve this walk-through:
debounce_msis 2 seconds, so that you see each change at once (withfeatures, the default is 10 minutes). See chatty documents.wait_for_childrenmakes the contract wait for its clauses’ answers (see wait for the clauses).
one_sided answer as a word beside the clause’s heading, and a clause with no answer yet shows no answer field.
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 whoseflagthreshold onone_sidedis true (a clause with no answer does not count).
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.
pending.
Read the contract’s first answer
Read the contract until its answer isfresh. 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).
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:
freshnessisfreshwhen the answer agrees with the contract, each clause answer it read, and the active version.pis the engine’s probability until a fit with features applies, and from then on it is the corrected number, withfitnaming the fit.thresholdsholdsreviewandmany_one_sided, read by the current thresholds against this answer.featuresholdsclauses.count(3 here) andclauses.count_where, as this evaluation read them.watermarksays how far into the namespace’s writes Brussle had read when it read the clauses, andanswers_generationhow far it had read the answers ofone_sided. A newer clause answer that changes what the contract shows makes the answerpending.evaluation_idnames 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_reachedorjudging_paused),failedorunavailable, because no new answer is on its way. - Brussle waits at most
max_wait_msafter the oldest change it has not judged (10 minutes when no ceiling is in effect), then judges the contract with the answers it has.
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 backfillone_sided.
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.- Brussle judges the clause again, and its
one_sidedanswer moves. The new text usually crosses into theone-sidedband, andflagbecomes true. - The new answer changes what the contract’s relation shows, so the contract’s answer becomes
pending. - Brussle judges the contract again after the contract’s
debounce_ms, so the engine sees the new band andclauses.count_wheregoes up by one.
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_whereormatch - the band that it gives to a
minor amax - its
latestvalue
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 itsreview 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.
["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
syncingwhile its first check records the contracts that match now, with no events, and then it islive. - 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
includenames, and neverstate(see event fields). - A threshold change on
one_sided, or a new fit oncontract_terms, changes many contracts at once, so by default the subscription sends onesubscription.syncedfor 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.
related_documents: each clause that the engine read, with its revision and theone_sidedevaluation behind its answer (see the audit). A missing clause was not in the relation’s selection, so check itsattributes.contract_idand the relation’smatch.upstream: present when a clause was behind and the contract read its last answer (see wait for the clauses).context_truncated:truewhen the context passedmax_tokensor the engine’s limit and Brussle cut part of it, so the engine did not see every selected clause.relations.clauses.droppedcounts the records left out. Show fewer fields of each clause, cut long fields withmax_chars, or lowerlast_n.relations_capped:truewhen the relation stopped before it checked every matching document, so the counts and the context may be missing a clause.relations.clausesshowscapped,range_fullorwalk_exhausted. Narrow the relation’smatchon attributes.relations.clauses: the clauses the relationscanned,selectedandrendered, and theirtokens. Ifselectedis lower than you expect, checklast_nandwindow.answers_generationandwatermark: 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.
The audit
Each entry ofrelated_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
fitsthe fit under which Brussle read itsp. - 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.