brainchain-verify 0.5.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +75 -7
- package/dist/cli.js +40 -19
- package/dist/verify.d.ts +64 -2
- package/dist/verify.js +691 -29
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -6,6 +6,7 @@ Check any BrainChain decision against the chain. The tool reads the decision fro
|
|
|
6
6
|
npx brainchain-verify mainnet 1
|
|
7
7
|
npx brainchain-verify mainnet --task 11 --set v3-1
|
|
8
8
|
npx brainchain-verify testnet 100 --json
|
|
9
|
+
npx brainchain-verify mainnet --receipt receipt.json
|
|
9
10
|
```
|
|
10
11
|
|
|
11
12
|
Requires Node.js 22 or later.
|
|
@@ -13,8 +14,9 @@ Requires Node.js 22 or later.
|
|
|
13
14
|
## Usage
|
|
14
15
|
|
|
15
16
|
```text
|
|
16
|
-
brainchain-verify <mainnet|testnet|chainId> <decisionId> [--set <TaskBoard>] [--rpc <url>] [--json]
|
|
17
|
-
brainchain-verify <mainnet|testnet|chainId> --task <taskId> [--set <TaskBoard>] [--rpc <url>] [--json]
|
|
17
|
+
brainchain-verify <mainnet|testnet|chainId> <decisionId> [--set <TaskBoard>] [--rpc <url>] [--offline] [--json]
|
|
18
|
+
brainchain-verify <mainnet|testnet|chainId> --task <taskId> [--set <TaskBoard>] [--rpc <url>] [--offline] [--json]
|
|
19
|
+
brainchain-verify <mainnet|testnet|chainId> [<decisionId>] --receipt <file|url> [--set <TaskBoard>] [--rpc <url>] [--json]
|
|
18
20
|
```
|
|
19
21
|
|
|
20
22
|
| Argument | Meaning |
|
|
@@ -24,6 +26,8 @@ brainchain-verify <mainnet|testnet|chainId> --task <taskId> [--set <TaskBoard>]
|
|
|
24
26
|
| `--task <id>` | Check every decision about one task, oldest first. |
|
|
25
27
|
| `--set <TaskBoard>` | A retired contract set from 3.0.0 on, by its TaskBoard address, by the key a decision page uses (`?set=v3-1`, or an older one the site still opens, such as `v4` for `v3-4`) or by release (`3.1.0`). Without it, the active set. Each set numbers its tasks and decisions from 1. The `v2` set is not supported: its board named tasks by id alone, which the subject check does not read. |
|
|
26
28
|
| `--rpc <url>` | Another RPC for the same chain. Records and briefs on private addresses are read only when this RPC is on one too. |
|
|
29
|
+
| `--receipt <file\|url>` | Check the receipt for a paid job, from a JSON file or an http(s) URL. Without a decision id, the tool checks the decision the receipt names, in the contract set its DecisionLog belongs to. See [receipts](#receipts). |
|
|
30
|
+
| `--offline` | Fetch no receipt from the brain host. A receipt given with `--receipt` is still checked. |
|
|
27
31
|
| `--json` | One JSON object on stdout. |
|
|
28
32
|
| `--version` | Print the version. |
|
|
29
33
|
|
|
@@ -68,27 +72,89 @@ The verdict is `match`, `withheld`, `UNVERIFIED` or `MISMATCH`. `withheld` means
|
|
|
68
72
|
| `input-hash` | `keccak256` of the record's `request`, serialized canonically (keys sorted, no whitespace), equals `inputHash`. |
|
|
69
73
|
| `kind` | The record's kind is the one on chain. |
|
|
70
74
|
| `subject` | A task decision names `TaskBoard.subjectOf(task)` for this chain and board. A change names its exact call. A grade of outside work is matched by the rules below. |
|
|
71
|
-
| `options` | The probabilities cover exactly the options in the question. |
|
|
75
|
+
| `options` | The probabilities cover exactly the options in the question. A panel that decided without Jev records none of Jev's, and `panel` checks that it names Jev as missing. |
|
|
72
76
|
| `answer` | The record's answer encodes to the one on chain: the hired model's id or worker address, or 1 for accept, pay, adopt and allow and 0 for their opposites. |
|
|
73
77
|
| `confidence` | The record's confidence times 10,000, rounded, equals `confidenceBps`. |
|
|
74
78
|
| `top-option` | For a hire, ruling or change, the answer is Jev's most likely option. |
|
|
75
|
-
| `confidence-recomputed` | The confidence follows from the probabilities. A hire uses `(p - 1/n) / (1 - 1/n)`, within 0.02 because the probabilities are rounded to two decimals. A grade sums the probabilities on the paid levels. A change uses the chosen option's probability, and so does a ruling recorded before rulings were graded twice. A grade or ruling with a `second` or `checks` field follows the two-grader rule: the probability of paying is the lower of Jev's and the second grader's, zero when the brief's checks failed, and the answer and confidence follow it. |
|
|
79
|
+
| `confidence-recomputed` | The confidence follows from the probabilities. A hire uses `(p - 1/n) / (1 - 1/n)`, within 0.02 because the probabilities are rounded to two decimals. A grade sums the probabilities on the paid levels. A change uses the chosen option's probability, and so does a ruling recorded before rulings were graded twice. A grade or ruling with a `second` or `checks` field follows the two-grader rule: the probability of paying is the lower of Jev's and the second grader's, zero when the brief's checks failed, and the answer and confidence follow it. A record with a `panel` field follows the panel's rule instead, as [decided by a panel](#decided-by-a-panel) describes. |
|
|
76
80
|
| `second-grader` | On a decision graded twice: the second grader's probability of paying recomputes from its probabilities, and the receipt id it carries is shown. |
|
|
81
|
+
| `judge` | On a decision by a panel, one line for each judge after the second grader: its probability of paying recomputes from its probabilities, which cover the question's options, and the receipt id it carries is shown. |
|
|
82
|
+
| `panel` | On a decision by a panel: the candidate rules recompute from the judges' probabilities of paying, the record's rule is one of them, and a panel short of a member says which and pays only from 90%. |
|
|
77
83
|
| `brief` | The question quotes the task's brief, fetched from its `Posted` event and checked against `briefHash`. Bytes that miss the hash are a `FAIL`; a host that cannot be read is a `skip`. |
|
|
84
|
+
| `checks` | A brief can carry checks: a fenced block marked `python checks` or `javascript checks`. The record of a grade or ruling on that brief holds BrainChain's run of them after the delivered code, and the line names the run's language and outcome. A run that failed, timed out, found no code in that language or was too long to run makes the answer reject, or refund on a ruling, at 10,000 bps. A run that passed is in the question the graders read, in BrainChain's own wording. A task record with checks in its brief and no run is a `FAIL`. |
|
|
78
85
|
| `levels` | A grade or ruling used the brief's own quality levels and pass level. On a grade of outside work, the pass level is a whole number naming one of the question's levels above the lowest. |
|
|
79
86
|
| `threshold` | The task's threshold, applied as `TaskBoard.hire`, `grade`, `rule` and `ruleAppeal` do, gives the outcome the board emitted. A poster can raise the grade and ruling thresholds while the task is open, so they can be higher than when it was posted; they cannot change after the hire. A ruling on appeal (set 3.5.0 and later) is checked against the board's `AppealRuled` event. A change is queued only on a yes. |
|
|
80
87
|
| `change-subject`, `change-call` | A queued change is the call the decision names and the call Jev was shown. |
|
|
88
|
+
| `evidence` | The record's evidence hash recomputes from the chain's `inputHash` and the record's judges, payments and versions, and equals the hash its `Recorded` event carries in `payloadURI`. |
|
|
89
|
+
| `versions` | The key the record names sent the decision's `Recorded` transaction, and the image and app it names are the ones the DecisionLog attested that key for. |
|
|
90
|
+
| `receipt` | The receipt's EIP-712 signature recovers to the key that sent the decision's `Recorded` transaction. |
|
|
91
|
+
| `receipt-decision` | The receipt names this decision, its DecisionLog, its chain and the record's evidence hash. |
|
|
92
|
+
| `receipt-outcome` | The receipt's outcome is the board's for a task, or the grade bar's for a paid grade. |
|
|
93
|
+
| `receipt-payment` | The receipt's payment is one the record names, and the payment transaction moved its amount from the payer to the desk. |
|
|
81
94
|
|
|
82
95
|
Some checks skip by design:
|
|
83
96
|
|
|
84
97
|
- A decision the board never applied, because a later decision or a closed window settled the task, skips `threshold`.
|
|
85
98
|
- A grade recorded before the chain gated on the paid levels skips `confidence-recomputed`: its confidence is Jev's score confidence, which the probabilities alone do not give.
|
|
99
|
+
- A grade or ruling recorded before BrainChain ran a brief's checks skips `checks` when its brief carries some. Such a record has no `jevPaid`, which every later grade and ruling states unless its panel decided without Jev.
|
|
100
|
+
- A grade of outside work skips `checks` when its brief ends in a checks block and its record holds no run. The section on grades of outside work says why.
|
|
101
|
+
|
|
102
|
+
### Decided by a panel
|
|
103
|
+
|
|
104
|
+
A grade or ruling can be put to a panel: Jev, the second grader and a third judge, each asked the same question from the same state. Its record then has a `panel` field beside `second`. A record from Jev and the second grader under the lower-of rule has no `panel` field and is checked as before.
|
|
105
|
+
|
|
106
|
+
On a record with a panel, the tool checks that:
|
|
107
|
+
|
|
108
|
+
- each judge's probability of paying recomputes from its own probabilities, measured as Jev's is, and matches its `paid`. Jev's matches `jevPaid`.
|
|
109
|
+
- each candidate in `rules` recomputes over the judges that answered. `lower-of` is the lowest probability. `majority` is the lowest within the surest majority, which is the median with three judges. `median` is the middle probability, or the mean of the middle two.
|
|
110
|
+
- `rules` holds exactly `lower-of`, `majority` and `median`, and `rule` names one of them.
|
|
111
|
+
- the answer and confidence follow from that rule's value: accept or pay from 0.5, at the probability of the answer, and reject or refund at 100% when the brief's checks failed.
|
|
112
|
+
- each judge's receipt id is shown, as for the second grader.
|
|
113
|
+
|
|
114
|
+
A panel of three can decide without a member that stopped answering. `panel.degraded` then names the missing member and gives the bar, which must be 0.9: the panel pays only from 90%, and below it the answer is reject or refund. At least two judges must have answered. When the second grader is missing, the record has no `second`. When Jev is missing, the record holds none of Jev's probabilities and no `jevPaid`, and `model` names the version BrainChain asked for, which `degraded.missing` must name too.
|
|
115
|
+
|
|
116
|
+
### Evidence and versions
|
|
117
|
+
|
|
118
|
+
A record made since BrainChain kept evidence has three more fields. `versions` names the version of the questions Jev was asked, the key that recorded the decision, and the enclave image and app the DecisionLog attested that key for. `payments` lists what an agent paid for the job through BrainChain. `evidence` is one hash over what the decision rests on. A record made before has none of the three and is checked as it always was.
|
|
119
|
+
|
|
120
|
+
`evidence` is the `keccak256` of `{ input, judges, payments, versions }` in canonical form. `input` is the decision's `inputHash` on chain, in lowercase. `judges` is Jev's `{ model, probabilities }`, then `second` and each entry of `panel.judges` as `{ model, probabilities, requestHash, receipt }`. `payments` is the record's, or `[]`. None of these parts is withheld with the work, so the hash is rebuilt on a copy served without it too.
|
|
121
|
+
|
|
122
|
+
The chain holds the hash too. A decision's `payloadURI` in its `Recorded` event ends in `#evidence=` and the hash, written in the same transaction as the decision. The rebuilt hash and the record's `evidence` must both equal it, and a `payloadURI` with any other fragment fails. The tool fetches the record without the fragment, as every HTTP client does, so the location names the same file. A decision recorded before the hash went on chain has no fragment, and its rebuilt hash is held to the record's own field.
|
|
123
|
+
|
|
124
|
+
`versions.brain` must be the account that sent the decision's `Recorded` transaction. Only the log's brain can record, so that is the key that recorded it. Its image and app must be the ones in the latest `BrainAttested` event for that key at or before the decision's block. A key the owner named with `setBrain` has no such event, and its records name no image or app. `versions.prompt` is shown. It is the version of the questions on [How Jev is asked](https://docs.usebrainchain.com/how-jev-is-asked), which nothing on chain records.
|
|
125
|
+
|
|
126
|
+
### Receipts
|
|
127
|
+
|
|
128
|
+
Every job an agent pays BrainChain for gets a receipt, signed by the key that records decisions. A paid grade gets one once Jev decides it. A paid task gets one once the board pays or refunds it, for the grade or ruling it settled on. The receipt is EIP-712 under the domain `BrainChain receipt`, version 1, on the DecisionLog, in the envelope of the x402 offer-and-receipt extension (version 0.6): `{ format, payload, signature }`.
|
|
129
|
+
|
|
130
|
+
For each paid grade or ruling, the tool asks the host that serves the record for its receipt at `/receipts/<DecisionLog in lowercase>/<decision id>.json`. A host that has none yet is a `skip`. `--offline` turns this off, and `--receipt` checks a receipt you kept instead.
|
|
131
|
+
|
|
132
|
+
On a receipt, the tool checks that:
|
|
133
|
+
|
|
134
|
+
- the signature recovers to the account that sent the decision's `Recorded` transaction, and `payload.brain` names it. The key comes from the transaction, so a receipt still checks after a release names a new key in `brain()`.
|
|
135
|
+
- `chainId`, `decisionLog`, `decision` and `evidence` are this chain, this DecisionLog, this decision and the record's evidence hash.
|
|
136
|
+
- for a task, the board paid it for `accepted` and refunded it for `rejected`, and the last grade or ruling it applied is this decision. For a paid grade, `accepted` means accept at or above the board's grade bar when the decision was recorded.
|
|
137
|
+
- `network`, `asset`, `amount`, `payer` and `transaction` are those of a payment in the record, and on that network the transaction moved `amount` of `asset` from `payer` to the desk. The desk is the `payTo` for that network in the brain host's `/network.json`, which the tool believes only when `brain()` on the active DecisionLog signed it. A desk's former addresses count on EVM networks.
|
|
138
|
+
|
|
139
|
+
The tool reads payments on Base, Robinhood Chain and Solana, and on their testnets, from their public RPCs. When `/network.json` or the payment's network cannot be read, `receipt-payment` skips and says why.
|
|
140
|
+
|
|
141
|
+
### Keeping a receipt
|
|
142
|
+
|
|
143
|
+
The reply to `POST /x402/grades` carries the receipt in its `receipt` field once the grade is decided. `GET /x402/tasks/<order>` carries it once the task is paid or refunded. Save the reply, or the receipt alone, as JSON:
|
|
144
|
+
|
|
145
|
+
```sh
|
|
146
|
+
npx brainchain-verify mainnet --receipt reply.json
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
The receipt names its decision and its DecisionLog, so the command needs no id, and a receipt on a retired set opens that set. With an id, the receipt is held to that decision. The host serves the same receipt at `https://brain.usebrainchain.com/receipts/<DecisionLog>/<decision id>.json`, at the same address after the contracts change, so a URL works as well as a file.
|
|
150
|
+
|
|
151
|
+
The reply that delivers a paid grade also carries the extension's own receipt in its `PAYMENT-RESPONSE` header, under `extensions["offer-receipt"].info.receipt`, signed by the same key. `--receipt` reads it too. It names no decision, evidence, outcome or amount, so give the decision's id: the tool checks its signer and its network, payer and transaction, and skips `receipt-decision`.
|
|
86
152
|
|
|
87
153
|
### Records served without the work
|
|
88
154
|
|
|
89
155
|
Work stays private until it is accepted, so a grade or ruling on work that was not accepted is served with the work taken out and the header `x-brainchain-withheld: result`. That copy cannot match `payloadHash` or `inputHash`, so `record-hash` and `input-hash` skip and the verdict is `withheld`, never `match`. Exit status stays 0.
|
|
90
156
|
|
|
91
|
-
The header alone proves nothing, so the tool honours it only under the `withheld` check above. The record's answer and confidence are still held to the chain, and for a task so are its brief, levels and threshold.
|
|
157
|
+
The header alone proves nothing, so the tool honours it only under the `withheld` check above. The record's answer and confidence are still held to the chain, and for a task so are its brief, levels and threshold. On a decision whose `payloadURI` carries its evidence hash, every judge's answer, Jev's included, the payments and the versions are held to that hash on chain, so a copy whose judges, payments or versions changed fails `evidence`. The question is the host's word. On an older decision, nothing ties the rest of the copy to the chain: the question, the split of the probabilities behind the confidence, the second grader and any other judge are all the host's word. The chain's rules are the records service's own: a task's work is public once it is paid or graded accepted (a house task's once graded), a paid grade's once accepted, and the delivery in an ACP evaluation never.
|
|
92
158
|
|
|
93
159
|
### Grades of outside work
|
|
94
160
|
|
|
@@ -99,6 +165,8 @@ A grade of work delivered outside BrainChain has no task on the board. The recor
|
|
|
99
165
|
|
|
100
166
|
Its answer and confidence are checked, and the confidence is recomputed from the record's own levels and pass level. Nothing on chain says what those levels should be, so `levels` skips and `threshold` skips: `payloadHash` commits them, but no brief does. The caller who paid for the grade received its verdict directly.
|
|
101
167
|
|
|
168
|
+
A paid grade runs the checks sent with its request. BrainChain appends them to the brief as its last block, and `checks` holds their run to the same rule as a task's. A caller can also write such a block into the brief's own text, where nothing runs it, and the record reads the same either way. So a brief that ends in a checks block with no run is a `skip`. An ACP evaluation runs no checks.
|
|
169
|
+
|
|
102
170
|
## What it cannot check
|
|
103
171
|
|
|
104
172
|
That Jev produced the answer. BrainChain records Jev's answers, and the contracts bound what a recorded answer can do. A decision that passes every check has the question, answer and probabilities the chain acted on. The [trust model](https://docs.usebrainchain.com/trust-model) explains what the chain relies on and what bounds it.
|
|
@@ -108,12 +176,12 @@ That Jev produced the answer. BrainChain records Jev's answers, and the contract
|
|
|
108
176
|
| Code | Meaning |
|
|
109
177
|
| --- | --- |
|
|
110
178
|
| 0 | Every check that could run passed. Skips do not fail a decision. A `withheld` verdict also exits 0, so read the verdict if you need to tell it from a full match. |
|
|
111
|
-
| 1 | A check failed, a record could not be fetched, or the decision could not be read: an unknown id, a task with no decisions, an RPC error. |
|
|
179
|
+
| 1 | A check failed, a record could not be fetched, or the decision could not be read: an unknown id, a task with no decisions, an RPC error, a `--receipt` that cannot be read. |
|
|
112
180
|
| 2 | Bad arguments, or an RPC that serves another chain. |
|
|
113
181
|
|
|
114
182
|
## JSON
|
|
115
183
|
|
|
116
|
-
`--json` prints one object. `decisions` holds one entry, or one per decision with `--task`. `verdict` is `match`, `withheld`, `mismatch` or `unverified` (a record could not be fetched). With `withheld`, `withheldNotChecked` says what was left out, and the decision carries `withheld: true`. A grade of outside work carries `outside: true`. Each check keeps its `name` across releases, and its `status` is `ok`, `fail`, `skip` or `error`. When the tool cannot run, it prints `{ "error": "..." }` and exits 1. Trimmed to one check:
|
|
184
|
+
`--json` prints one object. `decisions` holds one entry, or one per decision with `--task`. `verdict` is `match`, `withheld`, `mismatch` or `unverified` (a record could not be fetched). With `withheld`, `withheldNotChecked` says what was left out, and the decision carries `withheld: true`. A grade of outside work carries `outside: true`. A grade or ruling that holds a run of its brief's checks carries `run`, with the run's `language` and `outcome`. A grade or ruling decided by a panel carries `panel`: its `rule`, `judges` with each judge that answered by `model` and `paid`, Jev first, and `degraded`. A degraded panel also carries `missing` and `bar`. These are the record's own values, and the `judge` and `panel` checks say whether they recompute. A decision whose record matched the evidence hash on chain carries it as `evidence`. A decision whose receipt was checked carries `receipt`: where it was read as `from`, and the `job` and `outcome` it states. Each check keeps its `name` across releases, and its `status` is `ok`, `fail`, `skip` or `error`. When the tool cannot run, it prints `{ "error": "..." }` and exits 1, or 2 for bad arguments or an RPC that serves another chain. Trimmed to one check:
|
|
117
185
|
|
|
118
186
|
```json
|
|
119
187
|
{
|
package/dist/cli.js
CHANGED
|
@@ -3,16 +3,20 @@ import { createRequire } from "node:module";
|
|
|
3
3
|
import { createPublicClient, http, isAddress } from "viem";
|
|
4
4
|
import { decisionLogAbi, taskBoardAbi } from "./abi.js";
|
|
5
5
|
import { NETWORKS } from "./deployments.js";
|
|
6
|
-
import { clean, decisionsOf, errorMessage, outcomeOf, verifyDecision, withheldNotChecked } from "./verify.js";
|
|
6
|
+
import { clean, decisionsOf, errorMessage, outcomeOf, readReceipt, receiptDecision, verifyDecision, withheldNotChecked, } from "./verify.js";
|
|
7
7
|
const USAGE = `Usage:
|
|
8
|
-
brainchain-verify <mainnet|testnet|chainId> <decisionId> [--set <TaskBoard>] [--rpc <url>] [--json]
|
|
9
|
-
brainchain-verify <mainnet|testnet|chainId> --task <taskId> [--set <TaskBoard>] [--rpc <url>] [--json]
|
|
8
|
+
brainchain-verify <mainnet|testnet|chainId> <decisionId> [--set <TaskBoard>] [--rpc <url>] [--offline] [--json]
|
|
9
|
+
brainchain-verify <mainnet|testnet|chainId> --task <taskId> [--set <TaskBoard>] [--rpc <url>] [--offline] [--json]
|
|
10
|
+
brainchain-verify <mainnet|testnet|chainId> [<decisionId>] --receipt <file|url> [--set <TaskBoard>] [--rpc <url>] [--json]
|
|
10
11
|
|
|
11
12
|
Checks a BrainChain decision against the chain: reads it from the DecisionLog,
|
|
12
13
|
fetches the published record, recomputes its hashes and confidence, and applies
|
|
13
14
|
the task's thresholds as the contracts do. Needs no keys.
|
|
14
15
|
|
|
15
16
|
--task <id> check every decision about one task
|
|
17
|
+
--receipt <r> check the receipt for a paid job, from a file or a URL, against
|
|
18
|
+
the decision it names, or the one given
|
|
19
|
+
--offline fetch no receipt from the brain host
|
|
16
20
|
--set <board> a retired contract set from 3.0.0 on, by TaskBoard address, by
|
|
17
21
|
its key on the site (v3-1) or by release (3.1.0); without it, the
|
|
18
22
|
active set
|
|
@@ -27,6 +31,8 @@ const TRUST_MODEL = "https://docs.usebrainchain.com/trust-model";
|
|
|
27
31
|
// Sets before 3.0.0 named tasks by id alone, which the subject check does not model, so the package carries none.
|
|
28
32
|
const BEFORE_3 = /^(v2(-|$)|2\.|0x83b31d99fa31a9e5257955a737ef34c40f1c57bf$)/i;
|
|
29
33
|
function usage(message) {
|
|
34
|
+
if (json)
|
|
35
|
+
fail(message, 2);
|
|
30
36
|
console.error(`${message}\n\n${USAGE}`);
|
|
31
37
|
process.exit(2);
|
|
32
38
|
}
|
|
@@ -49,12 +55,18 @@ const flag = (name) => {
|
|
|
49
55
|
args.splice(at, 2);
|
|
50
56
|
return value;
|
|
51
57
|
};
|
|
52
|
-
const
|
|
53
|
-
|
|
54
|
-
|
|
58
|
+
const toggle = (name) => {
|
|
59
|
+
const at = args.indexOf(name);
|
|
60
|
+
if (at >= 0)
|
|
61
|
+
args.splice(at, 1);
|
|
62
|
+
return at >= 0;
|
|
63
|
+
};
|
|
64
|
+
const json = toggle("--json");
|
|
65
|
+
const offline = toggle("--offline");
|
|
55
66
|
const taskArg = flag("--task");
|
|
56
67
|
const setArg = flag("--set");
|
|
57
68
|
const rpcArg = flag("--rpc");
|
|
69
|
+
const receiptArg = flag("--receipt");
|
|
58
70
|
// -1 is a bad id, not an option; the id check below says so.
|
|
59
71
|
const unknown = args.find((arg) => arg.startsWith("-") && !/^-\d/.test(arg));
|
|
60
72
|
if (unknown)
|
|
@@ -66,10 +78,12 @@ if (networkArg === undefined)
|
|
|
66
78
|
usage("Name a network: mainnet, testnet or a chain id");
|
|
67
79
|
if (extra.length > 0 || (taskArg !== undefined && idArg !== undefined))
|
|
68
80
|
usage("Too many arguments");
|
|
69
|
-
|
|
70
|
-
|
|
81
|
+
if (taskArg !== undefined && receiptArg !== undefined)
|
|
82
|
+
usage("A receipt is for one decision: name it, not a task");
|
|
83
|
+
if (taskArg === undefined && idArg === undefined && receiptArg === undefined)
|
|
71
84
|
usage("Name a decision id, or a task with --task");
|
|
72
|
-
|
|
85
|
+
const given = taskArg ?? idArg;
|
|
86
|
+
if (given !== undefined && !/^[1-9]\d*$/.test(given))
|
|
73
87
|
usage(`${taskArg === undefined ? "A decision" : "A task"} id is a whole number from 1`);
|
|
74
88
|
const foundNetwork = Object.hasOwn(NETWORKS, networkArg)
|
|
75
89
|
? NETWORKS[networkArg]
|
|
@@ -77,8 +91,16 @@ const foundNetwork = Object.hasOwn(NETWORKS, networkArg)
|
|
|
77
91
|
if (!foundNetwork)
|
|
78
92
|
usage(`Unknown network "${networkArg}": use mainnet, testnet, 274663 or 274630`);
|
|
79
93
|
const network = foundNetwork;
|
|
94
|
+
const client = createPublicClient({ transport: http(rpcArg ?? network.rpc) });
|
|
95
|
+
const receipt = receiptArg === undefined ? undefined : await readReceipt(client, receiptArg).catch((error) => fail(errorMessage(error)));
|
|
96
|
+
const named = receipt && receiptDecision(receipt.value);
|
|
97
|
+
const target = given ?? named?.decision;
|
|
98
|
+
if (target === undefined || !/^[1-9]\d*$/.test(target))
|
|
99
|
+
usage("The receipt names no decision; give the decision's id");
|
|
100
|
+
// A receipt names its log, so it opens a retired set on its own.
|
|
101
|
+
const receiptSet = named && network.sets.find((s) => s.log.toLowerCase() === named.decisionLog.toLowerCase());
|
|
80
102
|
const foundSet = setArg === undefined
|
|
81
|
-
? network.sets[0]
|
|
103
|
+
? (receiptSet ?? network.sets[0])
|
|
82
104
|
: network.sets.find((s) => (isAddress(setArg) ? s.board.toLowerCase() === setArg.toLowerCase() : s.key === setArg || s.release === setArg || s.aliases?.includes(setArg)));
|
|
83
105
|
if (!foundSet) {
|
|
84
106
|
if (setArg !== undefined && BEFORE_3.test(setArg))
|
|
@@ -87,18 +109,13 @@ if (!foundSet) {
|
|
|
87
109
|
usage(`No contract set ${setArg} on ${network.name}. Sets: ${sets.join(", ")}`);
|
|
88
110
|
}
|
|
89
111
|
const set = foundSet;
|
|
90
|
-
const ctx = {
|
|
91
|
-
|
|
92
|
-
network,
|
|
93
|
-
set,
|
|
94
|
-
briefs: new Map(),
|
|
95
|
-
};
|
|
96
|
-
function fail(message) {
|
|
112
|
+
const ctx = { client, network, set, briefs: new Map(), offline };
|
|
113
|
+
function fail(message, status = 1) {
|
|
97
114
|
if (json)
|
|
98
115
|
console.log(JSON.stringify({ error: clean(message) }, null, 2));
|
|
99
116
|
else
|
|
100
117
|
console.error(clean(message));
|
|
101
|
-
process.exit(
|
|
118
|
+
process.exit(status);
|
|
102
119
|
}
|
|
103
120
|
const TAGS = { ok: "ok", fail: "FAIL", skip: "skip", error: "ERROR" };
|
|
104
121
|
const tally = (checks, status) => checks.filter((c) => c.status === status).length;
|
|
@@ -134,6 +151,10 @@ function report(reports, header, task) {
|
|
|
134
151
|
task: r.task,
|
|
135
152
|
...(r.outside ? { outside: true } : {}),
|
|
136
153
|
...(r.withheld ? { withheld: true } : {}),
|
|
154
|
+
...(r.run ? { run: r.run } : {}),
|
|
155
|
+
...(r.panel ? { panel: r.panel } : {}),
|
|
156
|
+
...(r.evidence ? { evidence: r.evidence } : {}),
|
|
157
|
+
...(r.receipt ? { receipt: r.receipt } : {}),
|
|
137
158
|
block: r.block,
|
|
138
159
|
recordedAt: r.recordedAt,
|
|
139
160
|
confidenceBps: r.confidenceBps,
|
|
@@ -193,7 +214,7 @@ try {
|
|
|
193
214
|
const hint = others.length > 0 ? ` Each set numbers its own decisions; try --set ${others.join(" or ")}.` : "";
|
|
194
215
|
fail(`DecisionLog ${set.log} (set ${set.key}) holds decisions 1 to ${decisions}.${hint}`);
|
|
195
216
|
}
|
|
196
|
-
report([await verifyDecision(ctx, id)], `Decision ${id} ${where} (DecisionLog ${set.log})`);
|
|
217
|
+
report([await verifyDecision(ctx, id, receipt)], `Decision ${id} ${where} (DecisionLog ${set.log})`);
|
|
197
218
|
}
|
|
198
219
|
catch (error) {
|
|
199
220
|
fail(errorMessage(error));
|
package/dist/verify.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type PublicClient } from "viem";
|
|
1
|
+
import { type Address, type Hex, type PublicClient } from "viem";
|
|
2
2
|
import type { ContractSet, Network } from "./deployments.js";
|
|
3
3
|
/** `error` is a check that could not complete, such as a record host that did not answer. */
|
|
4
4
|
export type Status = "ok" | "fail" | "skip" | "error";
|
|
@@ -8,6 +8,24 @@ export interface Check {
|
|
|
8
8
|
status: Status;
|
|
9
9
|
message: string;
|
|
10
10
|
}
|
|
11
|
+
/** The sandbox's run of a brief's checks after the delivered code. */
|
|
12
|
+
export interface Run {
|
|
13
|
+
language: "python" | "javascript";
|
|
14
|
+
outcome: "passed" | "failed" | "timed out";
|
|
15
|
+
}
|
|
16
|
+
/** A grade or ruling decided by a panel, as its record states it. */
|
|
17
|
+
export interface PanelReport {
|
|
18
|
+
rule: string;
|
|
19
|
+
/** Every member that answered, Jev first, with its probability of paying. */
|
|
20
|
+
judges: {
|
|
21
|
+
model: string;
|
|
22
|
+
paid: number;
|
|
23
|
+
}[];
|
|
24
|
+
degraded: boolean;
|
|
25
|
+
/** When degraded: the members the panel decided without, and what it then had to put on paying to pay. */
|
|
26
|
+
missing?: string[];
|
|
27
|
+
bar?: number;
|
|
28
|
+
}
|
|
11
29
|
export interface DecisionReport {
|
|
12
30
|
id: number;
|
|
13
31
|
kind?: string;
|
|
@@ -16,17 +34,53 @@ export interface DecisionReport {
|
|
|
16
34
|
outside?: boolean;
|
|
17
35
|
/** Served without the work, as the chain shows it may be, so its hashes could not be checked. */
|
|
18
36
|
withheld?: boolean;
|
|
37
|
+
/** On a grade or ruling whose request holds one. */
|
|
38
|
+
run?: Run;
|
|
39
|
+
panel?: PanelReport;
|
|
19
40
|
block?: number;
|
|
20
41
|
recordedAt?: string;
|
|
21
42
|
confidenceBps?: number;
|
|
22
43
|
payloadURI?: string;
|
|
44
|
+
/** The evidence hash the decision's Recorded event carries in payloadURI, once the record matched it. */
|
|
45
|
+
evidence?: Hex;
|
|
46
|
+
/** The receipt checked for the paid job this decision settled, with the job and outcome it states. */
|
|
47
|
+
receipt?: {
|
|
48
|
+
from: string;
|
|
49
|
+
job?: string;
|
|
50
|
+
outcome?: string;
|
|
51
|
+
};
|
|
23
52
|
checks: Check[];
|
|
24
53
|
}
|
|
54
|
+
/** A payment behind the job a decision is about, as its record and its receipt state it. */
|
|
55
|
+
export interface Payment {
|
|
56
|
+
/** CAIP-2, such as eip155:8453. */
|
|
57
|
+
network: string;
|
|
58
|
+
asset: string;
|
|
59
|
+
/** In the asset's smallest units. */
|
|
60
|
+
amount: string;
|
|
61
|
+
payer: string;
|
|
62
|
+
transaction: string;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* How a transaction changed each owner's balance of `asset`, by owner, in
|
|
66
|
+
* lowercase on EVM networks. Undefined when it is not on `network` or did not
|
|
67
|
+
* succeed.
|
|
68
|
+
*/
|
|
69
|
+
export type Balances = (network: string, transaction: string, asset: string) => Promise<Map<string, bigint> | undefined>;
|
|
25
70
|
export interface Context {
|
|
26
71
|
client: PublicClient;
|
|
27
72
|
network: Network;
|
|
28
73
|
set: ContractSet;
|
|
29
74
|
briefs: Map<bigint, Promise<Brief>>;
|
|
75
|
+
/** Reads payment transactions; public RPCs for Base, Robinhood Chain and Solana when absent. */
|
|
76
|
+
balances?: Balances;
|
|
77
|
+
/** Fetches no receipt from the brain host, so only one given to verifyDecision is checked. */
|
|
78
|
+
offline?: boolean;
|
|
79
|
+
}
|
|
80
|
+
/** A receipt as an agent kept it, and where it was read. */
|
|
81
|
+
export interface GivenReceipt {
|
|
82
|
+
from: string;
|
|
83
|
+
value: unknown;
|
|
30
84
|
}
|
|
31
85
|
interface Brief {
|
|
32
86
|
title: string;
|
|
@@ -40,7 +94,15 @@ export declare const clean: (text: string) => string;
|
|
|
40
94
|
export declare function errorMessage(error: unknown): string;
|
|
41
95
|
/** Decision ids about one task on the set's board, oldest first. */
|
|
42
96
|
export declare function decisionsOf({ client, network, set }: Context, task: bigint): Promise<bigint[]>;
|
|
43
|
-
|
|
97
|
+
/** `receipt` is one the caller kept. Without it, the receipt of a paid grade or ruling is fetched from the host that serves its record, unless the context is offline. */
|
|
98
|
+
export declare function verifyDecision(ctx: Context, id: bigint, receipt?: GivenReceipt): Promise<DecisionReport>;
|
|
99
|
+
/** Reads a receipt from a file or an http(s) URL: the receipt itself, or a reply that carries it as `receipt`. */
|
|
100
|
+
export declare function readReceipt(client: PublicClient, source: string): Promise<GivenReceipt>;
|
|
101
|
+
/** The decision and log a BrainChain receipt names. The x402 extension's receipt names neither. */
|
|
102
|
+
export declare function receiptDecision(value: unknown): {
|
|
103
|
+
decision: string;
|
|
104
|
+
decisionLog: Address;
|
|
105
|
+
} | undefined;
|
|
44
106
|
export type Outcome = "match" | "withheld" | "unverified" | "mismatch";
|
|
45
107
|
/** Disagreement first, then what could not be read. A withheld record never reads as a match: its hashes were not checked. */
|
|
46
108
|
export declare function outcomeOf(reports: DecisionReport[]): Outcome;
|