brainchain-verify 0.1.3 → 0.2.1

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 CHANGED
@@ -68,7 +68,8 @@ Each line is one check. `ok` passed and `FAIL` means the record and the chain di
68
68
  | `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. |
69
69
  | `confidence` | The record's confidence times 10,000, rounded, equals `confidenceBps`. |
70
70
  | `top-option` | For a hire, ruling or change, the answer is Jev's most likely option. |
71
- | `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 ruling or change uses the chosen option's probability. |
71
+ | `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. |
72
+ | `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. |
72
73
  | `brief` | The question quotes the task's brief, fetched from its `Posted` event and checked against `briefHash`. |
73
74
  | `levels` | A grade or ruling used the brief's own quality levels and pass level. |
74
75
  | `threshold` | The threshold fixed when the task was posted, applied as `TaskBoard.hire`, `grade` and `rule` do, gives the outcome the board emitted. A change is queued only on a yes. |
@@ -35,6 +35,14 @@ export const NETWORKS = {
35
35
  "name": "BrainChain testnet",
36
36
  "rpc": "https://rpc.testnet.usebrainchain.com",
37
37
  "sets": [
38
+ {
39
+ "key": "v3-4",
40
+ "release": "3.4.1",
41
+ "fromBlock": 2141,
42
+ "board": "0x67e0C5F884E1df61AC585B6d158c6E8175956AD1",
43
+ "log": "0x49019b7ad5B4b688A1bF3c52D9f3a1DF5870F59f",
44
+ "executor": "0x5BFbAf3986E4aFaeb2895535E0bE206F76e5EC63"
45
+ },
38
46
  {
39
47
  "key": "v3-4",
40
48
  "release": "3.4.0",
package/dist/verify.js CHANGED
@@ -10,6 +10,9 @@ const NO_ANSWERS = new Set(["reject", "refund", "keep", "refuse"]);
10
10
  // TypeSafe rounds probabilities and its own choice confidence to two decimals,
11
11
  // so a hire's confidence recomputed from the probabilities can differ slightly.
12
12
  const ROUNDING = 0.02;
13
+ // The brain keeps a probability of paying to four decimals.
14
+ const share = (parts) => Math.min(1, Number(parts.reduce((sum, p) => sum + p, 0).toFixed(4)));
15
+ const twice = (record) => record.second !== undefined || record.checks !== undefined;
13
16
  const ADDRESS = /^0x[0-9a-f]{40}$/i;
14
17
  const short = (value) => `${value.slice(0, 6)}…${value.slice(-4)}`;
15
18
  // Staked workers answer by address, which reads better shortened.
@@ -254,7 +257,7 @@ export async function verifyDecision(ctx, id) {
254
257
  c.check("answer", expectedAnswer?.toLowerCase() === decision.answer, `answer: ${named(record.answer)}, recorded as ${short(decision.answer)}`, `the record answers "${record.answer}"; the chain recorded ${decision.answer}`);
255
258
  c.check("confidence", Math.round(record.confidence * 10_000) === decision.confidenceBps, `confidence: ${record.confidence} is ${decision.confidenceBps} bps, as recorded`, `the record's confidence ${record.confidence} is ${Math.round(record.confidence * 10_000)} bps; the chain recorded ${decision.confidenceBps}`);
256
259
  const chosen = record.probabilities[record.answer];
257
- if (decision.kind !== Kind.Grade) {
260
+ if (decision.kind !== Kind.Grade && !(decision.kind === Kind.Ruling && twice(record))) {
258
261
  const top = Math.max(...Object.values(record.probabilities));
259
262
  c.check("top-option", chosen !== undefined && chosen >= top - 1e-9, `${named(record.answer)} is Jev's most likely option, at ${chosen}`, `${named(record.answer)} is not Jev's most likely option`);
260
263
  }
@@ -264,7 +267,10 @@ export async function verifyDecision(ctx, id) {
264
267
  const recomputed = (p - 1 / n) / (1 - 1 / n);
265
268
  c.check("confidence-recomputed", Math.abs(recomputed - record.confidence) <= ROUNDING, `confidence recomputed: (${p} - 1/${n}) / (1 - 1/${n}) = ${recomputed.toFixed(4)}, within rounding of ${record.confidence}`, `confidence recomputed from the probabilities is ${recomputed.toFixed(4)}, not ${record.confidence}`);
266
269
  }
267
- if (decision.kind === Kind.Ruling || decision.kind === Kind.Change) {
270
+ if (decision.kind === Kind.Ruling && twice(record)) {
271
+ checkPaying(c, record, { jev: record.probabilities.pay ?? 0, second: record.second?.probabilities.pay, yes: "pay", no: "refund" });
272
+ }
273
+ else if (decision.kind === Kind.Ruling || decision.kind === Kind.Change) {
268
274
  c.check("confidence-recomputed", record.confidence === chosen, `confidence recomputed: the probability of ${record.answer}, ${chosen}`, `confidence ${record.confidence} is not the probability of ${record.answer}`);
269
275
  }
270
276
  const state = (parseJson(record.request.state) ?? {});
@@ -307,10 +313,42 @@ async function checkBrief(ctx, c, task, decision, record, state) {
307
313
  }
308
314
  const paidKeys = ascending.map((_, i) => String(i)).slice(ascending.length - 1 - brief.passIndex);
309
315
  const parts = paidKeys.map((key) => record.probabilities[key] ?? 0);
310
- const paid = Math.min(1, parts.reduce((sum, p) => sum + p, 0));
311
- const accept = paid >= 0.5;
312
- const confidence = accept ? paid : 1 - paid;
313
- c.check("confidence-recomputed", Math.abs(confidence - record.confidence) < 1e-9 && record.answer === (accept ? "accept" : "reject"), `confidence recomputed: paid levels ${paidKeys.join(", ")} hold ${parts.join(" + ")} = ${paid.toFixed(4)}, so ${accept ? "accept" : "reject"} at ${confidence.toFixed(4)}`, `recomputed from the probabilities, the grade is ${accept ? "accept" : "reject"} at ${confidence.toFixed(4)}, not ${record.answer} at ${record.confidence}`);
316
+ const jev = share(parts);
317
+ const how = `paid levels ${paidKeys.join(", ")} hold ${parts.join(" + ")} = ${jev.toFixed(4)}`;
318
+ if (!twice(record)) {
319
+ const accept = jev >= 0.5;
320
+ const confidence = accept ? jev : 1 - jev;
321
+ c.check("confidence-recomputed", Math.abs(confidence - record.confidence) < 1e-6 && record.answer === (accept ? "accept" : "reject"), `confidence recomputed: ${how}, so ${accept ? "accept" : "reject"} at ${confidence.toFixed(4)}`, `recomputed from the probabilities, the grade is ${accept ? "accept" : "reject"} at ${confidence.toFixed(4)}, not ${record.answer} at ${record.confidence}`);
322
+ return;
323
+ }
324
+ const second = record.second ? share(paidKeys.map((key) => record.second.probabilities[key] ?? 0)) : undefined;
325
+ checkPaying(c, record, { jev, second, yes: "accept", no: "reject", how });
326
+ }
327
+ /**
328
+ * The rule grades and rulings share since they are graded twice: the
329
+ * probability of paying is the lower of Jev's and the second grader's, zero
330
+ * when the brief's checks failed, and the answer and confidence follow it.
331
+ */
332
+ function checkPaying(c, record, p) {
333
+ const steps = [p.how ?? `Jev puts ${p.jev} on ${p.yes}`];
334
+ let paying = p.jev;
335
+ if (record.second) {
336
+ c.check("second-grader", p.second !== undefined && Math.abs(p.second - record.second.paid) < 1e-6 && Math.abs((record.jevPaid ?? p.jev) - p.jev) < 1e-6, `the second grader (${record.second.model}) puts ${p.second} on paying${record.second.receipt ? `, with receipt ${record.second.receipt.id}` : ", without a receipt"}`, `the second grader's probability of paying recomputes to ${p.second}, not the recorded ${record.second.paid}`);
337
+ if (p.second !== undefined) {
338
+ paying = Math.min(paying, p.second);
339
+ steps.push(`the second grader holds ${p.second.toFixed(4)}, so the lower, ${paying.toFixed(4)}, counts`);
340
+ }
341
+ }
342
+ if (record.checks === "failed") {
343
+ paying = 0;
344
+ steps.push("the brief's checks failed on the delivered code, so the probability of paying is 0");
345
+ }
346
+ else if (record.checks === "passed") {
347
+ steps.push("the brief's checks passed");
348
+ }
349
+ const yes = paying >= 0.5;
350
+ const confidence = yes ? paying : 1 - paying;
351
+ c.check("confidence-recomputed", Math.abs(confidence - record.confidence) < 1e-6 && record.answer === (yes ? p.yes : p.no), `confidence recomputed: ${steps.join("; ")}, so ${yes ? p.yes : p.no} at ${confidence.toFixed(4)}`, `recomputed under the two-grader rule, the decision is ${yes ? p.yes : p.no} at ${confidence.toFixed(4)}, not ${record.answer} at ${record.confidence}`);
314
352
  }
315
353
  // Applies each threshold the way TaskBoard.hire, grade and rule do, to the
316
354
  // terms fixed when the task was posted, then compares with what the board did.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "brainchain-verify",
3
- "version": "0.1.3",
3
+ "version": "0.2.1",
4
4
  "description": "Check a BrainChain decision against the chain: the published record, its hashes, its confidence and the thresholds the contracts applied.",
5
5
  "license": "MIT",
6
6
  "author": "BrainChain",