CultNet governance contracts: vote API (sealed motions) and fork-desk card, with schemas, specs, vectors and C#/TS conformance
Some checks failed
CultMesh QUIC native / Build win32-x64 (pull_request) Has been skipped
CultMesh QUIC native / Build linux-x64 (pull_request) Has been skipped
CultMesh QUIC native / Release native binaries (pull_request) Has been skipped
CultMesh QUIC native / Verify committed binaries match SHA256SUMS (pull_request) Has been cancelled
CultMesh portability and performance / ubuntu-latest reactive semantics and leases (pull_request) Has been cancelled
Some checks failed
CultMesh QUIC native / Build win32-x64 (pull_request) Has been skipped
CultMesh QUIC native / Build linux-x64 (pull_request) Has been skipped
CultMesh QUIC native / Release native binaries (pull_request) Has been skipped
CultMesh QUIC native / Verify committed binaries match SHA256SUMS (pull_request) Has been cancelled
CultMesh portability and performance / ubuntu-latest reactive semantics and leases (pull_request) Has been cancelled
contracts/cultnet/governance: vote-api.{md,schema.json}, fork-card.{md,schema.json}, vectors for both (positive, negative per refusal, byte-exact MessagePack), generator scripts/gen-governance-vectors.py. Fork rules follow /opt/commons/handoff/fork-desk-answer-binding-20261110.md (options_hash canonical JSON with NFC, desk_answer tagged outcome, retraction, second door).
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
55bf180647
commit
2d921adba2
9 changed files with 10979 additions and 0 deletions
7422
contracts/cultnet/governance/fork-card-vectors.json
Normal file
7422
contracts/cultnet/governance/fork-card-vectors.json
Normal file
File diff suppressed because one or more lines are too long
142
contracts/cultnet/governance/fork-card.md
Normal file
142
contracts/cultnet/governance/fork-card.md
Normal file
|
|
@ -0,0 +1,142 @@
|
|||
# CultNet fork-desk contract v0 (specification)
|
||||
|
||||
Status: specification, drafted for Heimdall (Bifrost receiver), Huginn (the second door) and Void (Ratatoskr desk). No runtime implements it yet.
|
||||
Rule source: `/opt/commons/handoff/fork-desk-answer-binding-20261110.md` (Heimdall; sections 1.1 to 1.5, 1.3a to 1.3d), amended by the Persona briefs of 2026-11-10 (Eureka's fit check). Where this page differs from that file it says so under "Differences from the binding file".
|
||||
Format source: the fork desk artifact in `/opt/commons/handoff/fork-desk-original/` (ruling `eureka-substrate:ruling:fork-desk-nibu-format`).
|
||||
Files here: `fork-card.schema.json` (one schema, a `$defs` entry per document), `fork-card-vectors.json`. Conformance: `tests/GameCult.Networking.Tests/GovernanceVectorTests.cs` (C#) and `packages/cultnet-ts/test/governance-vectors.test.ts` (TypeScript), both generated against vectors written by `scripts/gen-governance-vectors.py`. Each suite computes every hash with its own code and replays every scenario through a reference judge written from this page.
|
||||
|
||||
All documents are CultNet map-mode: every key written, nil where absent, closed schemas. Field names are snake_case as in the binding file and the mind's records; `schemaVersion` keeps CultNet's spelling (open choice 1). The bytes of every document are pinned in the vectors as `messagePackHex`.
|
||||
|
||||
## Documents
|
||||
|
||||
| schema id | what | who writes it |
|
||||
|---|---|---|
|
||||
| `gamecult.fork.card.v0` | the fork as shown | the publisher (a Mind's card projection) |
|
||||
| `gamecult.fork.answer_request.v0` | the answer a decider sends | the client; Bifrost forwards it unchanged to Huginn's second door |
|
||||
| `gamecult.desk_answer.v0` | the stored answer with one tagged outcome | Huginn only |
|
||||
| `gamecult.fork.answer_result.v0` | what the sender gets back for a stored answer | Bifrost, from the stored `desk_answer` |
|
||||
| `gamecult.fork.refusal.v0` | a body refused at the door; nothing stored | Bifrost |
|
||||
| `gamecult.fork.record.v0` | a fork's answers plus the derived counted answer | a projection of the stored `desk_answer`s |
|
||||
| `gamecult.fork.ruling_cite.v0` | the citation a ruling carries | the Mind admitting the ruling |
|
||||
|
||||
### card
|
||||
|
||||
`{schemaVersion, fork_id, title, context, options[{id, text}], recommendation, depends[], leans, flag, raised_in, deciders[], status, ruling, ruled_from[], order, options_hash}`.
|
||||
|
||||
- `fork_id`: the mind's `question` key, `<campaign>:question:<label>`.
|
||||
- **Hashed (the shown members):** `title`, `context`, `options` (each `{id, text}`, in shown order), `recommendation` (an option id, **never nil**: the stored `question` requires `recommended`), `depends` (strings, each the `campaign:kind:label` rendering of a stored reference, in shown order, empty when none).
|
||||
- **Not hashed:** `fork_id` (bound beside the hash), `leans` (an option id or nil), `flag`, `raised_in`, `deciders`, `status` (`open` or `closed`), `ruling` (the ruling id when closed), `ruled_from` (answer ids a ruling has been admitted from), `order`, and `options_hash` itself.
|
||||
- **`deciders` is read-only data.** The list is set outside the Mind, through Bifrost's admin path; the Mind reads it and the card projection copies it. A question's own `audience` may only narrow it. A Mind never writes it, so a Mind cannot add itself and answer its own fork.
|
||||
- Rules a schema cannot say, each with a negative vector: option ids are unique; `recommendation` and `leans` name an option; deciders are unique; a closed card names its `ruling`; `options_hash` equals the hash of the shown members.
|
||||
|
||||
### Options hash (canonical bytes)
|
||||
|
||||
`options_hash` = lowercase hex SHA-256 of the UTF-8 bytes of the following, byte for byte:
|
||||
|
||||
1. Build an object with exactly the five members `title`, `context`, `options`, `recommendation`, `depends`. Each option is an object with exactly `id` and `text`.
|
||||
2. **NFC-normalise every string, object keys included.** No trimming, no newline rewriting, no case folding. A string with a lone surrogate is not a card (it has no UTF-8). Normalisation stability makes NFC stable across Unicode versions for already-assigned code points; a runtime's own normaliser is trusted and the vectors include a decomposed pair and astral code points (open choice 2).
|
||||
3. Serialise as RFC 8785 (JCS): no whitespace; object keys sorted by UTF-16 code unit, which for these ASCII keys is `context, depends, options, recommendation, title` and, inside an option, `id, text`; array order is as shown (option order and `depends` order are part of the hash).
|
||||
4. Strings are written in double quotes; only `"` and `\` are backslash-escaped, U+0008, U+0009, U+000A, U+000C, U+000D as `\b \t \n \f \r`, every other code point below U+0020 as `\u00xx` in lowercase hex; everything else is written raw as UTF-8 (U+007F, U+2028, `<`, `&` and astral code points included).
|
||||
5. **Numbers and nulls:** the member set contains none. There is no number to canonicalise (a number anywhere in the five members is a defect) and `recommendation` is never nil, so no nil is hashed.
|
||||
|
||||
`fork_id` is not in the hash and is bound beside it in its own field, so one hash cannot be replayed under another fork. The publisher computes the hash and stores it in the card; the client recomputes it from what it rendered and refuses to send on a mismatch; the receiver recomputes from its stored fork, never from the client.
|
||||
|
||||
Vectors `optionsHash`: the seven of the binding file (`heimdall_A..G`), computed by the Python generator, by the C# suite and by the TypeScript suite independently, plus text, title, context and `depends`-order mutations, a case showing the unshown members do not move the hash, escapes and markup, and astral ids. Each suite also mutates every shown member, an option id included, and asserts the hash changes.
|
||||
|
||||
### answer_request
|
||||
|
||||
`{schemaVersion, fork_id, options_hash, answer, retracted, retracts, answered_at}`. `answer` is `{choice, note}` (at least one non-nil) or nil on a retraction. `choice` is an option id; `note` is the person's words, verbatim, 1 to 4000 characters, not blank. `answered_at` is the client's claim. **A request never carries `answerer` or `authority`**: both are stamped by Huginn (`answerer` from the verified subject the receiver passes; `authority` from the door's configuration). A body that names either key at any depth is refused whole, never stripped (`body_forbidden_field`); any other unknown key is `body_unknown_field`. The request type Bifrost sends over the second door is this document; it has no field for either key, so a client cannot supply them by construction, and Bifrost's scan is the first line, not the only one. How Huginn receives the verified subject is the door's authenticated context, not a body field (open choice 3). In map-mode `retracted` is always written (the binding file's optional `retracted` becomes `false`).
|
||||
|
||||
### desk_answer (the stored kind)
|
||||
|
||||
`{schemaVersion, answer_id, fork_id, outcome, of, retracts, code, options_hash, choice, note, answered_at, received_at, claim_in_future, answerer, authority}`, key `<campaign>:desk_answer:<question-label>.n<seq>`; `answer_id` is `n<seq>`, assigned by Huginn per question, and **an answer is named by its answer id** everywhere (`retracts`, `of`, `ruled_from`). One tagged `outcome`, each variant carrying only its own link:
|
||||
|
||||
- `Counted`: the answer that counts. `of`, `retracts`, `code` nil.
|
||||
- `Revision`: a later passing answer, kept, not counted while the first stands. `of` names the counted answer at the time.
|
||||
- `Retraction`: the stored form of `retracted: true` plus `retracts`. `retracts` names the answer retracted; `choice` and `note` nil. The illegal pair (a retraction with no target, or a target with no retraction) cannot be written.
|
||||
- `Refused`: any refusal from steps 3 to 7 or R1 to R3, with its `code`, stored too. `choice` and `note` keep what the body carried.
|
||||
|
||||
`received_at` is Huginn's admission time; "first" means first by admission order (the `n<seq>` ordinal), never by `answered_at`. `claim_in_future` is true when `answered_at` is more than five minutes after `received_at`: accepted but flagged. A body refused for its shape (steps 1 and 2, and an unknown fork, which has no question to key a record under) never reaches Huginn and is not stored; it gets a `refusal` and no answer id.
|
||||
|
||||
### Counting rules (in order; the first failure refuses with that code)
|
||||
|
||||
| step | rule | refusal | stored |
|
||||
|---|---|---|---|
|
||||
| 1 | verified session or bearer | `unauthenticated` (401) | no |
|
||||
| 2 | closed body; no `answerer`/`authority` at any depth; type-correct; not (no `answer` and not a retraction) | `body_forbidden_field`, `body_unknown_field`, `body_invalid` (400) | no |
|
||||
| 3 | `fork_id` is a stored fork | `fork_unknown` (404) | no |
|
||||
| 4 | `options_hash` equals the stored fork's hash | `stale_fork` (409) | **yes**; the result carries the current card |
|
||||
| 5 | the stamped `answerer` is on `deciders` | `not_a_decider` (403) | yes |
|
||||
| 6 | the fork is open (`status: open`) | `fork_closed` (409) | yes |
|
||||
| 7 | `choice`, if present, is an option id | `choice_invalid` (400) | yes |
|
||||
| 8 | first passing answer for `(fork_id, options_hash)` is `Counted`; later passing answers are `Revision`s naming the counted one | | yes |
|
||||
| 9 | a Matrix edit, redaction or reaction changes nothing | | no event is an answer |
|
||||
|
||||
**Retraction** (`retracted: true`, `retracts: <answer id>`, `answer: nil`, `options_hash` still carried, so step 4 applies) passes steps 1 to 6, then:
|
||||
|
||||
- R1 form: it carries no `answer`, it names a `Counted` or `Revision` answer of this fork under this `options_hash`; else `bad_retraction` (400). A retraction naming an answer already retracted is `already_retracted` (409). `retracts` on a body that is not a retraction is `bad_retraction`.
|
||||
- R2 who: the original answerer or any other decider; step 5 already requires a decider.
|
||||
- R3 until ruled: refused `ruled` (409) when the card's `ruled_from` names the answer. A ruling counts from the Mind admitting it; the fork may stay open after a ruling that only illustrates or sketches, which is the case `ruled` exists for. A closed fork refuses at step 6 as `fork_closed`.
|
||||
- R4 counted answer: retracting the counted answer removes it from the count and the next passing answer in admission order counts again. Retracting an answer that was not counted changes nothing. After every answer is retracted, a fresh answer is `Counted`. Records stay append-only.
|
||||
|
||||
**The counted answer is derived**, not stored: the first `Counted` or `Revision` answer under the card's current `options_hash` that no `Retraction` names. When the card changes, answers under the old hash stop counting and the next answer under the new hash is `Counted` (vectors `a_changed_card_...`). The `record` projection carries it as `counted_answer_id`.
|
||||
|
||||
### Operator authority
|
||||
|
||||
A ruling may carry `authority: Operator` only if it cites (`ruling_cite.answer_id`) a stored `desk_answer` whose outcome is `Counted` and whose stamped `authority` is `Operator` (or a stamped relay of her words from a mapped door, which this contract does not model). The citation makes R3 checkable: `ruled_from` is a field, not an inference. Negative vectors (`rulingCites`): no citation, a `Revision`, a `Refused` answer, a Counted answer by a non-operator, an answer that does not exist. Other authorities need no citation.
|
||||
|
||||
## Old format, the mind's record, and what maps to what
|
||||
|
||||
The old desk (`index.html`) read `questions/<id>` and wrote `answers/<id>` in a shared store. The mind's `question` record is the source in the new design; the card is its projection.
|
||||
|
||||
| old desk | mind `question` record | card |
|
||||
|---|---|---|
|
||||
| `qid`, `id` | key `campaign:question:label` | `fork_id` |
|
||||
| `title` | `title` | `title` |
|
||||
| `body` | `question` (the long text) | `context` |
|
||||
| `depends`: a free string | `depends`: a list of references | `depends`: list of `campaign:kind:label` strings |
|
||||
| `options[{key, label, text}]` | `options[{label, text}]` | `options[{id, text}]` |
|
||||
| option `key` | option `label` (a slug, the id) | option `id` |
|
||||
| option `label` (a human title) | none | **dropped**: not stored, not hashed; shown text is `text` |
|
||||
| `rec` (a key) | `recommended` (a label) | `recommendation` (an id), never nil |
|
||||
| `lean` ("Self leans" mark) | none | `leans` (an id or nil), not hashed |
|
||||
| `flag` (the provisional line) | none | `flag` (a string or nil), not hashed |
|
||||
| `status` open or closed, `ruling` | derived from the resolution (status is never written) | `status`, `ruling`, both a projection |
|
||||
| `order` | none (admission ordinal) | `order` |
|
||||
| none | `raised_in`, `asked_on` | `raised_in`; `asked_on` is not carried |
|
||||
| none | none | `deciders` (outside the Mind), `ruled_from`, `options_hash` |
|
||||
| answer `{choice: key or null, note, question, updatedAt}`, one mutable document per question | none | `answer_request` then an append-only `desk_answer` |
|
||||
| `note`: her words verbatim | | `answer.note`, verbatim |
|
||||
| "Clear pick": `choice` back to null so the note stands alone | | an `answer` with `choice` nil and `note` set, as the first answer or as a `Revision` |
|
||||
| "Changing an answer is fine until it's admitted as a ruling" | | a later answer is a `Revision`; `Retraction` withdraws one; neither changes who counted first, and `ruled` stops a retraction once a ruling is admitted |
|
||||
|
||||
Differences that need Void's choice: the old page **overwrites** one answer per question and the new store **appends**, so the booth cannot show "your answer" as one mutable field (it shows the latest answer by the person, plus its history); the old page needs no identity and the new one stamps `answerer` from a verified session; the old `label` has nowhere to live unless the card's `text` carries it.
|
||||
|
||||
## Transport note for Void: Matrix event versus how the mind carries questions
|
||||
|
||||
How a mind carries a question today: a typed `question` record in Huginn (`huginn view`, `huginn query`); the Persona relays it to the operator in chat, and the operator's reply comes back as chat text that Self reads and admits as a `ruling` in her words. Identity is whoever the chat attributes the message to; nothing binds an answer to the question text she saw.
|
||||
|
||||
Option A, **a Matrix event in the Mind's room with the answer as a reply.** Gains: it sits where people already are, needs no sign-in beyond Matrix, and the thread shows the exchange. Loses: the sender is an mxid, not a Bifrost account, so `answerer` and `authority` have no verified source; an edit or redaction changes the visible message but not the decision (binding rule 9), so the thread and the record can disagree; there is nowhere to carry `options_hash`, so a stale card cannot be refused; and a reply is free text, so `choice` is lost.
|
||||
|
||||
Option B, **Bifrost receives the answer and Huginn stores it** (the design in this page; Ratatoskr PR #24 also withdraws the Matrix carrier). Gains: a verified account, a bound hash, refusals that are stored and visible, one counted answer, retractions. Loses: the booth needs Bifrost sign-in, which does not reach chat yet (Bifrost PR #9 and #37 design it), so until then the desk is read-only; and the exchange is no longer in the room.
|
||||
|
||||
**Recommendation: B, with Matrix as presentation only.** The Mind may post a card as a listing and a link into the desk; no Matrix event is ever an answer (rule 9). Confidence: high on the rule (it is Heimdall's and Eureka's already), moderate on the interim, where read-only until sign-in is a real cost; if the operator needs to answer before sign-in reaches chat, the honest stopgap is the old path (chat text, Self admits a ruling in her words) rather than a Matrix reply the record cannot bind.
|
||||
|
||||
## Differences from the binding file
|
||||
|
||||
- Spelling: every field is as the binding file names it (`fork_id`, `options_hash`, `answered_at`, `retracted`, `retracts`, `answer.choice`, `answer.note`, `recommendation`, `depends`, option `id`), except that `schemaVersion` keeps CultNet's camelCase and `answer_id` replaces the earlier `receipt id` (Persona brief).
|
||||
- Map-mode writes every key: `retracted` is always present; nothing is "optional".
|
||||
- `Refused` stores every refusal from steps 3 to 7 and R1 to R3 (his section 1.3b); only steps 1 and 2 and `fork_unknown` are door-only. The binding file does not say whether `fork_unknown` is stored; it cannot be keyed without a question, so it is not (open choice 4).
|
||||
- The binding file's `choice_valid` has no named refusal code; this page names it `choice_invalid`. `fork_known` is `fork_unknown`. A type-invalid body is `body_invalid`.
|
||||
- The recommendation is never nil. The binding file's earlier vectors D and E used a nil recommendation; its updated D and E use `"a"`, as here.
|
||||
- Hash vectors: A, B, C, D, E, F and G of the binding file are reproduced digest for digest by the Python generator, the C# suite and the TypeScript suite (the generator prints `same` for each; none differs).
|
||||
|
||||
## Open choices
|
||||
|
||||
1. Case of field names: snake_case for the fork documents (as the binding file) beside camelCase for the vote documents (as Bifrost's serializer). One convention would be tidier.
|
||||
2. NFC across runtimes relies on each runtime's normaliser; a pinned table, as `write-receipt.md` does for letters, would remove the reliance. The risk is only for code points assigned after a runtime's Unicode version.
|
||||
3. How Huginn learns the verified subject: the second door's authenticated context (the peer credential names Bifrost the service, not the member), so the subject must travel beside the request. This contract has no envelope for it.
|
||||
4. Whether `fork_unknown` and shape refusals should leave an audit record somewhere other than a `desk_answer`.
|
||||
5. Whether `leans` and `flag`, which the booth shows, should join the hash. The binding file's five members do not include them; a changed lean would not make an answer stale.
|
||||
6. What the person sees of a retracted counted answer when the next answer counts: this contract only says the count moves.
|
||||
7. `received_at` is not in the binding file's stored fields; it is kept for `claim_in_future` and could be dropped if the ordinal alone decides order.
|
||||
756
contracts/cultnet/governance/fork-card.schema.json
Normal file
756
contracts/cultnet/governance/fork-card.schema.json
Normal file
|
|
@ -0,0 +1,756 @@
|
|||
{
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"$id": "https://github.com/GameCult/CultLib/contracts/cultnet/governance/fork-card.schema.json",
|
||||
"title": "CultNet fork-desk contract v0",
|
||||
"description": "A fork card, the answer request a decider sends, the stored desk_answer with its tagged outcome, the result and door refusals returned to the sender, the record a fork keeps, and the ruling citation. Map-mode: every key is written, nil where absent. A request never carries answerer or authority: Huginn stamps both. Rule source: /opt/commons/handoff/fork-desk-answer-binding-20261110.md. Spec: fork-card.md. Vectors: fork-card-vectors.json.",
|
||||
"$defs": {
|
||||
"hex64": {
|
||||
"type": "string",
|
||||
"pattern": "^[0-9a-f]{64}$"
|
||||
},
|
||||
"instant": {
|
||||
"type": "string",
|
||||
"pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}(\\.[0-9]{1,7})?Z$"
|
||||
},
|
||||
"id": {
|
||||
"type": "string",
|
||||
"minLength": 1
|
||||
},
|
||||
"answerId": {
|
||||
"type": "string",
|
||||
"pattern": "^n[1-9][0-9]*$"
|
||||
},
|
||||
"option": {
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"required": [
|
||||
"id",
|
||||
"text"
|
||||
],
|
||||
"properties": {
|
||||
"id": {
|
||||
"type": "string",
|
||||
"minLength": 1
|
||||
},
|
||||
"text": {
|
||||
"type": "string",
|
||||
"minLength": 1
|
||||
}
|
||||
}
|
||||
},
|
||||
"ref": {
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"required": [
|
||||
"kind",
|
||||
"id"
|
||||
],
|
||||
"properties": {
|
||||
"kind": {
|
||||
"type": "string",
|
||||
"minLength": 1
|
||||
},
|
||||
"id": {
|
||||
"type": "string",
|
||||
"minLength": 1
|
||||
}
|
||||
}
|
||||
},
|
||||
"card": {
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"required": [
|
||||
"schemaVersion",
|
||||
"fork_id",
|
||||
"title",
|
||||
"context",
|
||||
"options",
|
||||
"recommendation",
|
||||
"depends",
|
||||
"leans",
|
||||
"flag",
|
||||
"raised_in",
|
||||
"deciders",
|
||||
"status",
|
||||
"ruling",
|
||||
"ruled_from",
|
||||
"order",
|
||||
"options_hash"
|
||||
],
|
||||
"properties": {
|
||||
"schemaVersion": {
|
||||
"const": "gamecult.fork.card.v0"
|
||||
},
|
||||
"fork_id": {
|
||||
"$ref": "#/$defs/id"
|
||||
},
|
||||
"title": {
|
||||
"type": "string",
|
||||
"minLength": 1
|
||||
},
|
||||
"context": {
|
||||
"type": "string"
|
||||
},
|
||||
"options": {
|
||||
"type": "array",
|
||||
"minItems": 2,
|
||||
"items": {
|
||||
"$ref": "#/$defs/option"
|
||||
}
|
||||
},
|
||||
"recommendation": {
|
||||
"type": "string",
|
||||
"minLength": 1
|
||||
},
|
||||
"depends": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "string",
|
||||
"minLength": 1
|
||||
}
|
||||
},
|
||||
"leans": {
|
||||
"oneOf": [
|
||||
{
|
||||
"type": "string",
|
||||
"minLength": 1
|
||||
},
|
||||
{
|
||||
"type": "null"
|
||||
}
|
||||
]
|
||||
},
|
||||
"flag": {
|
||||
"oneOf": [
|
||||
{
|
||||
"type": "string",
|
||||
"minLength": 1
|
||||
},
|
||||
{
|
||||
"type": "null"
|
||||
}
|
||||
]
|
||||
},
|
||||
"raised_in": {
|
||||
"oneOf": [
|
||||
{
|
||||
"$ref": "#/$defs/ref"
|
||||
},
|
||||
{
|
||||
"type": "null"
|
||||
}
|
||||
]
|
||||
},
|
||||
"deciders": {
|
||||
"type": "array",
|
||||
"minItems": 1,
|
||||
"items": {
|
||||
"type": "string",
|
||||
"minLength": 1
|
||||
}
|
||||
},
|
||||
"status": {
|
||||
"enum": [
|
||||
"open",
|
||||
"closed"
|
||||
]
|
||||
},
|
||||
"ruling": {
|
||||
"oneOf": [
|
||||
{
|
||||
"type": "string",
|
||||
"minLength": 1
|
||||
},
|
||||
{
|
||||
"type": "null"
|
||||
}
|
||||
]
|
||||
},
|
||||
"ruled_from": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"$ref": "#/$defs/answerId"
|
||||
}
|
||||
},
|
||||
"order": {
|
||||
"type": "integer",
|
||||
"minimum": 0
|
||||
},
|
||||
"options_hash": {
|
||||
"$ref": "#/$defs/hex64"
|
||||
}
|
||||
}
|
||||
},
|
||||
"words": {
|
||||
"type": "string",
|
||||
"minLength": 1,
|
||||
"maxLength": 4000,
|
||||
"pattern": "^(?![\\t-\\r \\u0085 -
]+$)[\\s\\S]+$"
|
||||
},
|
||||
"answerObject": {
|
||||
"oneOf": [
|
||||
{
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"required": [
|
||||
"choice",
|
||||
"note"
|
||||
],
|
||||
"properties": {
|
||||
"choice": {
|
||||
"$ref": "#/$defs/id"
|
||||
},
|
||||
"note": {
|
||||
"$ref": "#/$defs/words"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"required": [
|
||||
"choice",
|
||||
"note"
|
||||
],
|
||||
"properties": {
|
||||
"choice": {
|
||||
"$ref": "#/$defs/id"
|
||||
},
|
||||
"note": {
|
||||
"type": "null"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"required": [
|
||||
"choice",
|
||||
"note"
|
||||
],
|
||||
"properties": {
|
||||
"choice": {
|
||||
"type": "null"
|
||||
},
|
||||
"note": {
|
||||
"$ref": "#/$defs/words"
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"answerRequest": {
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"required": [
|
||||
"schemaVersion",
|
||||
"fork_id",
|
||||
"options_hash",
|
||||
"answer",
|
||||
"retracted",
|
||||
"retracts",
|
||||
"answered_at"
|
||||
],
|
||||
"properties": {
|
||||
"schemaVersion": {
|
||||
"const": "gamecult.fork.answer_request.v0"
|
||||
},
|
||||
"fork_id": {
|
||||
"$ref": "#/$defs/id"
|
||||
},
|
||||
"options_hash": {
|
||||
"$ref": "#/$defs/hex64"
|
||||
},
|
||||
"answer": {
|
||||
"oneOf": [
|
||||
{
|
||||
"$ref": "#/$defs/answerObject"
|
||||
},
|
||||
{
|
||||
"type": "null"
|
||||
}
|
||||
]
|
||||
},
|
||||
"retracted": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"retracts": {
|
||||
"oneOf": [
|
||||
{
|
||||
"$ref": "#/$defs/answerId"
|
||||
},
|
||||
{
|
||||
"type": "null"
|
||||
}
|
||||
]
|
||||
},
|
||||
"answered_at": {
|
||||
"$ref": "#/$defs/instant"
|
||||
}
|
||||
}
|
||||
},
|
||||
"deskAnswerCounted": {
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"required": [
|
||||
"schemaVersion",
|
||||
"answer_id",
|
||||
"fork_id",
|
||||
"outcome",
|
||||
"of",
|
||||
"retracts",
|
||||
"code",
|
||||
"options_hash",
|
||||
"choice",
|
||||
"note",
|
||||
"answered_at",
|
||||
"received_at",
|
||||
"claim_in_future",
|
||||
"answerer",
|
||||
"authority"
|
||||
],
|
||||
"properties": {
|
||||
"schemaVersion": {
|
||||
"const": "gamecult.desk_answer.v0"
|
||||
},
|
||||
"answer_id": {
|
||||
"$ref": "#/$defs/answerId"
|
||||
},
|
||||
"fork_id": {
|
||||
"$ref": "#/$defs/id"
|
||||
},
|
||||
"outcome": {
|
||||
"const": "Counted"
|
||||
},
|
||||
"of": {
|
||||
"type": "null"
|
||||
},
|
||||
"retracts": {
|
||||
"type": "null"
|
||||
},
|
||||
"code": {
|
||||
"type": "null"
|
||||
},
|
||||
"options_hash": {
|
||||
"$ref": "#/$defs/hex64"
|
||||
},
|
||||
"choice": {
|
||||
"oneOf": [
|
||||
{
|
||||
"$ref": "#/$defs/id"
|
||||
},
|
||||
{
|
||||
"type": "null"
|
||||
}
|
||||
]
|
||||
},
|
||||
"note": {
|
||||
"oneOf": [
|
||||
{
|
||||
"$ref": "#/$defs/words"
|
||||
},
|
||||
{
|
||||
"type": "null"
|
||||
}
|
||||
]
|
||||
},
|
||||
"answered_at": {
|
||||
"$ref": "#/$defs/instant"
|
||||
},
|
||||
"received_at": {
|
||||
"$ref": "#/$defs/instant"
|
||||
},
|
||||
"claim_in_future": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"answerer": {
|
||||
"$ref": "#/$defs/id"
|
||||
},
|
||||
"authority": {
|
||||
"$ref": "#/$defs/id"
|
||||
}
|
||||
}
|
||||
},
|
||||
"deskAnswerRevision": {
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"required": [
|
||||
"schemaVersion",
|
||||
"answer_id",
|
||||
"fork_id",
|
||||
"outcome",
|
||||
"of",
|
||||
"retracts",
|
||||
"code",
|
||||
"options_hash",
|
||||
"choice",
|
||||
"note",
|
||||
"answered_at",
|
||||
"received_at",
|
||||
"claim_in_future",
|
||||
"answerer",
|
||||
"authority"
|
||||
],
|
||||
"properties": {
|
||||
"schemaVersion": {
|
||||
"const": "gamecult.desk_answer.v0"
|
||||
},
|
||||
"answer_id": {
|
||||
"$ref": "#/$defs/answerId"
|
||||
},
|
||||
"fork_id": {
|
||||
"$ref": "#/$defs/id"
|
||||
},
|
||||
"outcome": {
|
||||
"const": "Revision"
|
||||
},
|
||||
"of": {
|
||||
"$ref": "#/$defs/answerId"
|
||||
},
|
||||
"retracts": {
|
||||
"type": "null"
|
||||
},
|
||||
"code": {
|
||||
"type": "null"
|
||||
},
|
||||
"options_hash": {
|
||||
"$ref": "#/$defs/hex64"
|
||||
},
|
||||
"choice": {
|
||||
"oneOf": [
|
||||
{
|
||||
"$ref": "#/$defs/id"
|
||||
},
|
||||
{
|
||||
"type": "null"
|
||||
}
|
||||
]
|
||||
},
|
||||
"note": {
|
||||
"oneOf": [
|
||||
{
|
||||
"$ref": "#/$defs/words"
|
||||
},
|
||||
{
|
||||
"type": "null"
|
||||
}
|
||||
]
|
||||
},
|
||||
"answered_at": {
|
||||
"$ref": "#/$defs/instant"
|
||||
},
|
||||
"received_at": {
|
||||
"$ref": "#/$defs/instant"
|
||||
},
|
||||
"claim_in_future": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"answerer": {
|
||||
"$ref": "#/$defs/id"
|
||||
},
|
||||
"authority": {
|
||||
"$ref": "#/$defs/id"
|
||||
}
|
||||
}
|
||||
},
|
||||
"deskAnswerRetraction": {
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"required": [
|
||||
"schemaVersion",
|
||||
"answer_id",
|
||||
"fork_id",
|
||||
"outcome",
|
||||
"of",
|
||||
"retracts",
|
||||
"code",
|
||||
"options_hash",
|
||||
"choice",
|
||||
"note",
|
||||
"answered_at",
|
||||
"received_at",
|
||||
"claim_in_future",
|
||||
"answerer",
|
||||
"authority"
|
||||
],
|
||||
"properties": {
|
||||
"schemaVersion": {
|
||||
"const": "gamecult.desk_answer.v0"
|
||||
},
|
||||
"answer_id": {
|
||||
"$ref": "#/$defs/answerId"
|
||||
},
|
||||
"fork_id": {
|
||||
"$ref": "#/$defs/id"
|
||||
},
|
||||
"outcome": {
|
||||
"const": "Retraction"
|
||||
},
|
||||
"of": {
|
||||
"type": "null"
|
||||
},
|
||||
"retracts": {
|
||||
"$ref": "#/$defs/answerId"
|
||||
},
|
||||
"code": {
|
||||
"type": "null"
|
||||
},
|
||||
"options_hash": {
|
||||
"$ref": "#/$defs/hex64"
|
||||
},
|
||||
"choice": {
|
||||
"type": "null"
|
||||
},
|
||||
"note": {
|
||||
"type": "null"
|
||||
},
|
||||
"answered_at": {
|
||||
"$ref": "#/$defs/instant"
|
||||
},
|
||||
"received_at": {
|
||||
"$ref": "#/$defs/instant"
|
||||
},
|
||||
"claim_in_future": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"answerer": {
|
||||
"$ref": "#/$defs/id"
|
||||
},
|
||||
"authority": {
|
||||
"$ref": "#/$defs/id"
|
||||
}
|
||||
}
|
||||
},
|
||||
"deskAnswerRefused": {
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"required": [
|
||||
"schemaVersion",
|
||||
"answer_id",
|
||||
"fork_id",
|
||||
"outcome",
|
||||
"of",
|
||||
"retracts",
|
||||
"code",
|
||||
"options_hash",
|
||||
"choice",
|
||||
"note",
|
||||
"answered_at",
|
||||
"received_at",
|
||||
"claim_in_future",
|
||||
"answerer",
|
||||
"authority"
|
||||
],
|
||||
"properties": {
|
||||
"schemaVersion": {
|
||||
"const": "gamecult.desk_answer.v0"
|
||||
},
|
||||
"answer_id": {
|
||||
"$ref": "#/$defs/answerId"
|
||||
},
|
||||
"fork_id": {
|
||||
"$ref": "#/$defs/id"
|
||||
},
|
||||
"outcome": {
|
||||
"const": "Refused"
|
||||
},
|
||||
"of": {
|
||||
"type": "null"
|
||||
},
|
||||
"retracts": {
|
||||
"type": "null"
|
||||
},
|
||||
"code": {
|
||||
"enum": [
|
||||
"stale_fork",
|
||||
"not_a_decider",
|
||||
"fork_closed",
|
||||
"choice_invalid",
|
||||
"bad_retraction",
|
||||
"already_retracted",
|
||||
"ruled"
|
||||
]
|
||||
},
|
||||
"options_hash": {
|
||||
"$ref": "#/$defs/hex64"
|
||||
},
|
||||
"choice": {
|
||||
"oneOf": [
|
||||
{
|
||||
"$ref": "#/$defs/id"
|
||||
},
|
||||
{
|
||||
"type": "null"
|
||||
}
|
||||
]
|
||||
},
|
||||
"note": {
|
||||
"oneOf": [
|
||||
{
|
||||
"$ref": "#/$defs/words"
|
||||
},
|
||||
{
|
||||
"type": "null"
|
||||
}
|
||||
]
|
||||
},
|
||||
"answered_at": {
|
||||
"$ref": "#/$defs/instant"
|
||||
},
|
||||
"received_at": {
|
||||
"$ref": "#/$defs/instant"
|
||||
},
|
||||
"claim_in_future": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"answerer": {
|
||||
"$ref": "#/$defs/id"
|
||||
},
|
||||
"authority": {
|
||||
"$ref": "#/$defs/id"
|
||||
}
|
||||
}
|
||||
},
|
||||
"deskAnswer": {
|
||||
"oneOf": [
|
||||
{
|
||||
"$ref": "#/$defs/deskAnswerCounted"
|
||||
},
|
||||
{
|
||||
"$ref": "#/$defs/deskAnswerRevision"
|
||||
},
|
||||
{
|
||||
"$ref": "#/$defs/deskAnswerRetraction"
|
||||
},
|
||||
{
|
||||
"$ref": "#/$defs/deskAnswerRefused"
|
||||
}
|
||||
]
|
||||
},
|
||||
"answerResult": {
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"required": [
|
||||
"schemaVersion",
|
||||
"answer",
|
||||
"card"
|
||||
],
|
||||
"properties": {
|
||||
"schemaVersion": {
|
||||
"const": "gamecult.fork.answer_result.v0"
|
||||
},
|
||||
"answer": {
|
||||
"$ref": "#/$defs/deskAnswer"
|
||||
},
|
||||
"card": {
|
||||
"oneOf": [
|
||||
{
|
||||
"$ref": "#/$defs/card"
|
||||
},
|
||||
{
|
||||
"type": "null"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
"refusal": {
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"required": [
|
||||
"schemaVersion",
|
||||
"code",
|
||||
"error"
|
||||
],
|
||||
"properties": {
|
||||
"schemaVersion": {
|
||||
"const": "gamecult.fork.refusal.v0"
|
||||
},
|
||||
"code": {
|
||||
"enum": [
|
||||
"unauthenticated",
|
||||
"body_forbidden_field",
|
||||
"body_unknown_field",
|
||||
"body_invalid",
|
||||
"fork_unknown"
|
||||
]
|
||||
},
|
||||
"error": {
|
||||
"type": "string",
|
||||
"minLength": 1
|
||||
}
|
||||
}
|
||||
},
|
||||
"record": {
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"required": [
|
||||
"schemaVersion",
|
||||
"fork_id",
|
||||
"options_hash",
|
||||
"counted_answer_id",
|
||||
"answers"
|
||||
],
|
||||
"properties": {
|
||||
"schemaVersion": {
|
||||
"const": "gamecult.fork.record.v0"
|
||||
},
|
||||
"fork_id": {
|
||||
"$ref": "#/$defs/id"
|
||||
},
|
||||
"options_hash": {
|
||||
"$ref": "#/$defs/hex64"
|
||||
},
|
||||
"counted_answer_id": {
|
||||
"oneOf": [
|
||||
{
|
||||
"$ref": "#/$defs/answerId"
|
||||
},
|
||||
{
|
||||
"type": "null"
|
||||
}
|
||||
]
|
||||
},
|
||||
"answers": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"$ref": "#/$defs/deskAnswer"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"rulingCite": {
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"required": [
|
||||
"schemaVersion",
|
||||
"ruling_id",
|
||||
"authority",
|
||||
"answer_id"
|
||||
],
|
||||
"properties": {
|
||||
"schemaVersion": {
|
||||
"const": "gamecult.fork.ruling_cite.v0"
|
||||
},
|
||||
"ruling_id": {
|
||||
"type": "string",
|
||||
"minLength": 1
|
||||
},
|
||||
"authority": {
|
||||
"type": "string",
|
||||
"minLength": 1
|
||||
},
|
||||
"answer_id": {
|
||||
"oneOf": [
|
||||
{
|
||||
"$ref": "#/$defs/answerId"
|
||||
},
|
||||
{
|
||||
"type": "null"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
1067
contracts/cultnet/governance/vote-api-vectors.json
Normal file
1067
contracts/cultnet/governance/vote-api-vectors.json
Normal file
File diff suppressed because it is too large
Load diff
89
contracts/cultnet/governance/vote-api.md
Normal file
89
contracts/cultnet/governance/vote-api.md
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
# CultNet vote API v0 (specification)
|
||||
|
||||
Status: specification, drafted for Heimdall (Bifrost) and a Ratatoskr voting module. No server implements it yet.
|
||||
Files here: `vote-api.schema.json` (one schema, a `$defs` entry per document), `vote-api-vectors.json`. Conformance suites: `tests/GameCult.Networking.Tests/GovernanceVectorTests.cs` (C#) and `packages/cultnet-ts/test/governance-vectors.test.ts` (TypeScript). The vectors are written by `scripts/gen-governance-vectors.py`.
|
||||
Facts it rests on: `/opt/commons/handoff/bifrost-browser-voting-facts.md` (Bifrost `416f11a`; `416f11a..276f611` changes four files: a Standard-close concurrency fix in `MotionGovernanceService.cs`, two test files and `docs/deploy-main-to-live.md`; none touches the sealed API, so the facts hold at `276f611`) and Bifrost PR #37 (`docs/ratatoskr-voting-auth.md`, see "PR #37" below).
|
||||
|
||||
## Scope
|
||||
|
||||
Sealed-ballot motions only (`gamecult.sealed_ballot.v0`). Standard motions have no JSON read or cast API at all today, no Mind path, and no receipt; they are not in v0 (open choice 1). The contract names four exchanges and one refusal document:
|
||||
|
||||
| exchange | request | response |
|
||||
|---|---|---|
|
||||
| motion list | `gamecult.vote.motion_list_request.v0` | `gamecult.vote.motion_list.v0` |
|
||||
| motion read | `gamecult.vote.motion_read_request.v0` | `gamecult.vote.motion.v0` |
|
||||
| cast (commit or reveal) | `gamecult.vote.cast.v0` | `gamecult.vote.receipt.v0` |
|
||||
| any refusal | | `gamecult.vote.refusal.v0` |
|
||||
|
||||
Every document is CultNet map-mode: all keys are written, nil where absent, and the schema is closed (`additionalProperties: false`). Instants are RFC 3339 UTC with a `Z` and up to seven fractional digits. Ids are lowercase `D`-format UUIDs. The bytes of every document are pinned by `messagePackHex` in the vectors (shortest integers, shortest strings, keys in the order the schema lists them); JSON with the same keys is the xenos-boundary form of the same documents (open choice 4: the wire format).
|
||||
|
||||
## Documents
|
||||
|
||||
- **motion_list_request**: `{schemaVersion, phase}`; `phase` is `Commit`, `Reveal`, `Closed` or nil for all.
|
||||
- **motion_list**: `{schemaVersion, motions: [{id, title, round, phase, commitClosesAtUtc, closesAtUtc}]}`. **Heimdall must add this endpoint**: Bifrost has no list route for sealed motions today, only fetch-by-id. No pagination in v0 (open choice 2).
|
||||
- **motion_read_request**: `{schemaVersion, motionId}`.
|
||||
- **motion**: Bifrost's `SealedMotionState` with its names: `{schemaVersion, id, title, summary, round, priorMotionId, ballotSchema, phase, candidates (two or more), commitClosesAtUtc, closesAtUtc, yourVoterId, yourCommitment (a receipt or nil), announcement}`. It never carries a tally, a turnout, or another voter's commitment or ballot (a `tally` key is a negative vector). `yourVoterId` is the voter's `UserAccount.Id`, the `voterUserAccountId` of the commitment.
|
||||
- **cast**: `{schemaVersion, motionId, verb, commitment, ballot}` with exactly one of two shapes: `verb: "commit"` with a 64-hex `commitment` and `ballot: nil`, or `verb: "reveal"` with `commitment: nil` and a `ballot`. One document for both verbs; today's two routes (`/commit`, `/reveal`) may stay, taking this document as the body, with a `verb` that must match the route (open choice 3).
|
||||
- **ballot** (inside a reveal): `{schema: "gamecult.sealed_ballot.v0", motionId, round, candidate, reason, salt, persona}`. `candidate` and `reason` are single-line and **already trimmed** (no Unicode White_Space at either end, using the White_Space list of `write-receipt.md`); the contract refuses an untrimmed value rather than trimming it, so the bytes hashed are the bytes sent (Bifrost trims silently today: open choice 5). `reason` is 1 to 280 characters (UTF-16 code units, as Bifrost counts; the vectors keep the boundary cases to the BMP). `salt` is 32 to 128 lowercase hex. `persona` is `""` for a member and a printable-ASCII, space-free id of at most 64 characters for a Mind.
|
||||
- **receipt**: Bifrost's `SealedBallotReceipt`: `{schemaVersion, motionId, voterUserAccountId, commitment, revision (>= 1), committedAtUtc, revealed}`. The receipt for a reveal has `revealed: true` and **echoes nothing of the ballot** (no candidate, reason or salt; negative vectors for each).
|
||||
- **refusal**: `{schemaVersion, code, error, retryAfterSeconds}`; the `code` set is closed:
|
||||
|
||||
| code | HTTP | trigger |
|
||||
|---|---|---|
|
||||
| `unauthenticated` | 401 | no bearer, or one Heimdall does not know (the API must answer this, not redirect to a sign-in page) |
|
||||
| `forbidden` | 403 | authenticated but not an Active member, or a Mind key used on a route it may not call |
|
||||
| `not_found` | 404 | unknown motion id, a Standard motion id, or a reveal with no prior commit |
|
||||
| `wrong_phase` | 409 | commit outside Commit, reveal outside Reveal |
|
||||
| `contended` | 409 | a reveal raced the close fence: send it again |
|
||||
| `invalid` | 400 | the body fails the schema or an in-document rule (untrimmed text, candidate not in the motion, `round` or `motionId` mismatch, `persona` wrong for the credential) |
|
||||
| `commitment_mismatch` | 400 | a reveal whose canonical bytes do not hash to the stored commitment |
|
||||
| `payload_too_large` | 413 | body over 4096 bytes |
|
||||
| `rate_limited` | 429 | cast rate exceeded; `retryAfterSeconds >= 1`, mirrored in `Retry-After` |
|
||||
|
||||
Each code has a vector (`refusals`), and every failure of the schema has a negative vector (`invalid`).
|
||||
|
||||
## Commitment
|
||||
|
||||
`commitment = lowercase hex SHA-256 of the UTF-8 bytes of`
|
||||
|
||||
gamecult.sealed_ballot.v0 \n {motionId} \n {voterUserAccountId} \n {persona} \n {round} \n {candidate} \n {reason} \n {salt}
|
||||
|
||||
with `\n` U+000A, ids in `D` format, `round` in decimal, `persona` empty for a member. This is Bifrost's formula as the code has it (`SealedBallotService.cs:154, 385-387`); `docs/sealed-ballot-motion.md:22-23` omits `{persona}` and is stale. The vectors give the canonical bytes (`canonicalHex`) and the digest for a member, a Mind, non-ASCII text, and a decomposed-versus-precomposed pair (no normalisation: they are different ballots). `pairs` pins what a reveal must match: the right ballot matches; a changed reason, another voter id, or a dropped persona does not, and the answer is `commitment_mismatch`.
|
||||
|
||||
## Receipts the voter can check later
|
||||
|
||||
A cast returns the receipt. The voter checks it later in two places that must agree byte for byte with it: `motion.yourCommitment` on a motion read (a commit receipt is present from the commit; the revision counts re-commits), and, after close, the `commitment` the result lists for them. The contract adds no receipt id or signature: the receipt is the commitment, its revision and the time (open choice 6).
|
||||
|
||||
## Personas
|
||||
|
||||
A Persona votes only on sealed motions, through a Mind key (Bifrost binds an Ed25519 public key 1:1 to an Active member account). The ballot's `persona` is the bound persona id and the commitment covers it, so a Mind's ballot cannot be replayed as a member's (`pairs`: dropping the persona does not match). The Mind-intake route today takes a signed `MindEnvelope` anonymously; this contract does not model that envelope, nonce or signature (open choice 7). Standard motions have no Mind path.
|
||||
|
||||
## Authentication
|
||||
|
||||
An opaque `Authorization: Bearer` header. The token route, claims, scopes and audience belong to Heimdall and Bifrost and are not designed here. A request without a usable bearer is answered `unauthenticated` as a typed refusal.
|
||||
|
||||
## PR #37 (Bifrost `docs/ratatoskr-voting-auth.md`, head `447a7cb`)
|
||||
|
||||
Checked 2026-11-10 by an Eyes pass over the PR and the schema; conflicts for Heimdall:
|
||||
|
||||
1. **Standard motions.** The PR serves them (`PUT /api/votes/motions/{id}/ballot {choice, comment?}`, a receipt `{motionId, choice, revision, castAtUtc, receiptId}`); the contract is sealed-only and answers `not_found` for a Standard id.
|
||||
2. **Verb model.** The PR keeps `POST .../commit` and `.../reveal` and adds `GET .../result`; the contract has one `cast` document and no result document.
|
||||
3. **List and read as GET.** The PR names GET routes; the contract's request documents carry a `phase` filter and a `motionId`. The mapping (query string and path) is undefined until Heimdall fixes the routes.
|
||||
4. **Refusals.** The PR lists 401/403/404/409/429 with `WWW-Authenticate`; the contract adds 400 (`invalid`, `commitment_mismatch`) and 413, and a typed body.
|
||||
5. **Minds.** The PR says Minds never use `/api/votes` and keeps the anonymous signed `/mind` route; the contract carries `persona` in the ballot and a `forbidden` case for a Mind key on a route it may not call. The PR's token is never a Persona's.
|
||||
6. **Authority.** The contract's `unauthenticated` says a bearer "Heimdall does not know"; the PR makes Bifrost the OIDC issuer (a second client, scopes `votes.read` and `votes.cast`, audience `bifrost-votes`) and notes that Bifrost OIDC is not live (its D7).
|
||||
7. **Rate limits.** The PR keys per account (30 casts per 10 minutes); the contract says per credential.
|
||||
8. **Wire format.** The PR's errors are JSON; the contract's vectors are MessagePack. Nothing settles the media type.
|
||||
9. **The PR's seven open decisions** (consent for `votes.cast`, re-authentication, no refresh token, grant revocation, a Ratatoskr brake, rate numbers, MAS rollout order) assume nothing the contract contradicts. D2 (re-authentication at cast) would need a refusal code if answered yes; `unauthenticated` is the nearest.
|
||||
|
||||
## Open choices for Heimdall
|
||||
|
||||
1. Standard motions in v0 or a v1 (the PR already serves them).
|
||||
2. Pagination and filters on the list.
|
||||
3. One `cast` route or the two existing ones; `verb` redundancy.
|
||||
4. Wire format: MessagePack documents with a JSON export, or JSON only (the PR is JSON).
|
||||
5. Refuse untrimmed `candidate` and `reason` (this contract) or keep trimming server-side.
|
||||
6. Whether a receipt should carry an id or a server signature so a voter can prove it to a third party.
|
||||
7. The Mind path: keep the signed envelope on its own route, or take Mind keys as a bearer credential.
|
||||
8. `reason` length in UTF-16 code units (as Bifrost) or code points.
|
||||
9. Whether the result document (`GET .../result`) joins this contract.
|
||||
408
contracts/cultnet/governance/vote-api.schema.json
Normal file
408
contracts/cultnet/governance/vote-api.schema.json
Normal file
|
|
@ -0,0 +1,408 @@
|
|||
{
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"$id": "https://github.com/GameCult/CultLib/contracts/cultnet/governance/vote-api.schema.json",
|
||||
"title": "CultNet vote API v0 (sealed-ballot motions)",
|
||||
"description": "Typed documents for listing, reading and casting on sealed-ballot motions, and the receipt a cast returns. Map-mode: every key is written, nil where absent. Spec: vote-api.md. Vectors: vote-api-vectors.json.",
|
||||
"$defs": {
|
||||
"uuid": {
|
||||
"type": "string",
|
||||
"pattern": "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$"
|
||||
},
|
||||
"instant": {
|
||||
"type": "string",
|
||||
"pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}(\\.[0-9]{1,7})?Z$"
|
||||
},
|
||||
"hex64": {
|
||||
"type": "string",
|
||||
"pattern": "^[0-9a-f]{64}$"
|
||||
},
|
||||
"trimmedLine": {
|
||||
"type": "string",
|
||||
"minLength": 1,
|
||||
"pattern": "^(?![\\t-\\r \\u0085 -
])[^\\n\\r\\u0085
]+(?<![\\t-\\r \\u0085 -
])$"
|
||||
},
|
||||
"ballot": {
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"required": [
|
||||
"schema",
|
||||
"motionId",
|
||||
"round",
|
||||
"candidate",
|
||||
"reason",
|
||||
"salt",
|
||||
"persona"
|
||||
],
|
||||
"properties": {
|
||||
"schema": {
|
||||
"const": "gamecult.sealed_ballot.v0"
|
||||
},
|
||||
"motionId": {
|
||||
"$ref": "#/$defs/uuid"
|
||||
},
|
||||
"round": {
|
||||
"type": "integer",
|
||||
"minimum": 1
|
||||
},
|
||||
"candidate": {
|
||||
"$ref": "#/$defs/trimmedLine"
|
||||
},
|
||||
"reason": {
|
||||
"type": "string",
|
||||
"minLength": 1,
|
||||
"maxLength": 280,
|
||||
"pattern": "^(?![\\t-\\r \\u0085 -
])[^\\n\\r\\u0085
]+(?<![\\t-\\r \\u0085 -
])$"
|
||||
},
|
||||
"salt": {
|
||||
"type": "string",
|
||||
"pattern": "^[0-9a-f]{32,128}$"
|
||||
},
|
||||
"persona": {
|
||||
"type": "string",
|
||||
"pattern": "^[!-~]{0,64}$"
|
||||
}
|
||||
}
|
||||
},
|
||||
"motionListRequest": {
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"required": [
|
||||
"schemaVersion",
|
||||
"phase"
|
||||
],
|
||||
"properties": {
|
||||
"schemaVersion": {
|
||||
"const": "gamecult.vote.motion_list_request.v0"
|
||||
},
|
||||
"phase": {
|
||||
"oneOf": [
|
||||
{
|
||||
"enum": [
|
||||
"Commit",
|
||||
"Reveal",
|
||||
"Closed"
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "null"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
"motionListItem": {
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"required": [
|
||||
"id",
|
||||
"title",
|
||||
"round",
|
||||
"phase",
|
||||
"commitClosesAtUtc",
|
||||
"closesAtUtc"
|
||||
],
|
||||
"properties": {
|
||||
"id": {
|
||||
"$ref": "#/$defs/uuid"
|
||||
},
|
||||
"title": {
|
||||
"type": "string",
|
||||
"minLength": 1
|
||||
},
|
||||
"round": {
|
||||
"type": "integer",
|
||||
"minimum": 1
|
||||
},
|
||||
"phase": {
|
||||
"enum": [
|
||||
"Commit",
|
||||
"Reveal",
|
||||
"Closed"
|
||||
]
|
||||
},
|
||||
"commitClosesAtUtc": {
|
||||
"$ref": "#/$defs/instant"
|
||||
},
|
||||
"closesAtUtc": {
|
||||
"$ref": "#/$defs/instant"
|
||||
}
|
||||
}
|
||||
},
|
||||
"motionList": {
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"required": [
|
||||
"schemaVersion",
|
||||
"motions"
|
||||
],
|
||||
"properties": {
|
||||
"schemaVersion": {
|
||||
"const": "gamecult.vote.motion_list.v0"
|
||||
},
|
||||
"motions": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"$ref": "#/$defs/motionListItem"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"motionReadRequest": {
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"required": [
|
||||
"schemaVersion",
|
||||
"motionId"
|
||||
],
|
||||
"properties": {
|
||||
"schemaVersion": {
|
||||
"const": "gamecult.vote.motion_read_request.v0"
|
||||
},
|
||||
"motionId": {
|
||||
"$ref": "#/$defs/uuid"
|
||||
}
|
||||
}
|
||||
},
|
||||
"receipt": {
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"required": [
|
||||
"schemaVersion",
|
||||
"motionId",
|
||||
"voterUserAccountId",
|
||||
"commitment",
|
||||
"revision",
|
||||
"committedAtUtc",
|
||||
"revealed"
|
||||
],
|
||||
"properties": {
|
||||
"schemaVersion": {
|
||||
"const": "gamecult.vote.receipt.v0"
|
||||
},
|
||||
"motionId": {
|
||||
"$ref": "#/$defs/uuid"
|
||||
},
|
||||
"voterUserAccountId": {
|
||||
"$ref": "#/$defs/uuid"
|
||||
},
|
||||
"commitment": {
|
||||
"$ref": "#/$defs/hex64"
|
||||
},
|
||||
"revision": {
|
||||
"type": "integer",
|
||||
"minimum": 1
|
||||
},
|
||||
"committedAtUtc": {
|
||||
"$ref": "#/$defs/instant"
|
||||
},
|
||||
"revealed": {
|
||||
"type": "boolean"
|
||||
}
|
||||
}
|
||||
},
|
||||
"motion": {
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"required": [
|
||||
"schemaVersion",
|
||||
"id",
|
||||
"title",
|
||||
"summary",
|
||||
"round",
|
||||
"priorMotionId",
|
||||
"ballotSchema",
|
||||
"phase",
|
||||
"candidates",
|
||||
"commitClosesAtUtc",
|
||||
"closesAtUtc",
|
||||
"yourVoterId",
|
||||
"yourCommitment",
|
||||
"announcement"
|
||||
],
|
||||
"properties": {
|
||||
"schemaVersion": {
|
||||
"const": "gamecult.vote.motion.v0"
|
||||
},
|
||||
"id": {
|
||||
"$ref": "#/$defs/uuid"
|
||||
},
|
||||
"title": {
|
||||
"type": "string",
|
||||
"minLength": 1
|
||||
},
|
||||
"summary": {
|
||||
"type": "string"
|
||||
},
|
||||
"round": {
|
||||
"type": "integer",
|
||||
"minimum": 1
|
||||
},
|
||||
"priorMotionId": {
|
||||
"oneOf": [
|
||||
{
|
||||
"$ref": "#/$defs/uuid"
|
||||
},
|
||||
{
|
||||
"type": "null"
|
||||
}
|
||||
]
|
||||
},
|
||||
"ballotSchema": {
|
||||
"const": "gamecult.sealed_ballot.v0"
|
||||
},
|
||||
"phase": {
|
||||
"enum": [
|
||||
"Commit",
|
||||
"Reveal",
|
||||
"Closed"
|
||||
]
|
||||
},
|
||||
"candidates": {
|
||||
"type": "array",
|
||||
"minItems": 2,
|
||||
"items": {
|
||||
"$ref": "#/$defs/trimmedLine"
|
||||
}
|
||||
},
|
||||
"commitClosesAtUtc": {
|
||||
"$ref": "#/$defs/instant"
|
||||
},
|
||||
"closesAtUtc": {
|
||||
"$ref": "#/$defs/instant"
|
||||
},
|
||||
"yourVoterId": {
|
||||
"$ref": "#/$defs/uuid"
|
||||
},
|
||||
"yourCommitment": {
|
||||
"oneOf": [
|
||||
{
|
||||
"$ref": "#/$defs/receipt"
|
||||
},
|
||||
{
|
||||
"type": "null"
|
||||
}
|
||||
]
|
||||
},
|
||||
"announcement": {
|
||||
"oneOf": [
|
||||
{
|
||||
"type": "string"
|
||||
},
|
||||
{
|
||||
"type": "null"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
"commitCast": {
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"required": [
|
||||
"schemaVersion",
|
||||
"motionId",
|
||||
"verb",
|
||||
"commitment",
|
||||
"ballot"
|
||||
],
|
||||
"properties": {
|
||||
"schemaVersion": {
|
||||
"const": "gamecult.vote.cast.v0"
|
||||
},
|
||||
"motionId": {
|
||||
"$ref": "#/$defs/uuid"
|
||||
},
|
||||
"verb": {
|
||||
"const": "commit"
|
||||
},
|
||||
"commitment": {
|
||||
"$ref": "#/$defs/hex64"
|
||||
},
|
||||
"ballot": {
|
||||
"type": "null"
|
||||
}
|
||||
}
|
||||
},
|
||||
"revealCast": {
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"required": [
|
||||
"schemaVersion",
|
||||
"motionId",
|
||||
"verb",
|
||||
"commitment",
|
||||
"ballot"
|
||||
],
|
||||
"properties": {
|
||||
"schemaVersion": {
|
||||
"const": "gamecult.vote.cast.v0"
|
||||
},
|
||||
"motionId": {
|
||||
"$ref": "#/$defs/uuid"
|
||||
},
|
||||
"verb": {
|
||||
"const": "reveal"
|
||||
},
|
||||
"commitment": {
|
||||
"type": "null"
|
||||
},
|
||||
"ballot": {
|
||||
"$ref": "#/$defs/ballot"
|
||||
}
|
||||
}
|
||||
},
|
||||
"cast": {
|
||||
"oneOf": [
|
||||
{
|
||||
"$ref": "#/$defs/commitCast"
|
||||
},
|
||||
{
|
||||
"$ref": "#/$defs/revealCast"
|
||||
}
|
||||
]
|
||||
},
|
||||
"refusal": {
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"required": [
|
||||
"schemaVersion",
|
||||
"code",
|
||||
"error",
|
||||
"retryAfterSeconds"
|
||||
],
|
||||
"properties": {
|
||||
"schemaVersion": {
|
||||
"const": "gamecult.vote.refusal.v0"
|
||||
},
|
||||
"code": {
|
||||
"enum": [
|
||||
"unauthenticated",
|
||||
"forbidden",
|
||||
"not_found",
|
||||
"wrong_phase",
|
||||
"contended",
|
||||
"invalid",
|
||||
"commitment_mismatch",
|
||||
"payload_too_large",
|
||||
"rate_limited"
|
||||
]
|
||||
},
|
||||
"error": {
|
||||
"type": "string",
|
||||
"minLength": 1
|
||||
},
|
||||
"retryAfterSeconds": {
|
||||
"oneOf": [
|
||||
{
|
||||
"type": "integer",
|
||||
"minimum": 1
|
||||
},
|
||||
{
|
||||
"type": "null"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
295
packages/cultnet-ts/test/governance-vectors.test.ts
Normal file
295
packages/cultnet-ts/test/governance-vectors.test.ts
Normal file
|
|
@ -0,0 +1,295 @@
|
|||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { createHash } from "node:crypto";
|
||||
import { readFileSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
|
||||
import Ajv2020 from "ajv/dist/2020.js";
|
||||
import { decode, encode } from "@msgpack/msgpack";
|
||||
|
||||
// contracts/cultnet/governance: the vote-API and fork-card contracts (vote-api.md, fork-card.md). The vectors are spec-authored
|
||||
// (scripts/gen-governance-vectors.py); this suite re-encodes every message with this runtime and must reproduce the bytes, judges
|
||||
// every negative with the committed schema, and replays the fork scenarios through a reference judge written from fork-card.md.
|
||||
const DIR = join(__dirname, "..", "..", "..", "..", "contracts", "cultnet", "governance");
|
||||
const read = (name: string) => JSON.parse(readFileSync(join(DIR, name), "utf8"));
|
||||
const vote = read("vote-api-vectors.json");
|
||||
const fork = read("fork-card-vectors.json");
|
||||
|
||||
const ajv = new Ajv2020({ allErrors: true, strict: false });
|
||||
const voteSchema = read("vote-api.schema.json");
|
||||
const forkSchema = read("fork-card.schema.json");
|
||||
ajv.addSchema(voteSchema);
|
||||
ajv.addSchema(forkSchema);
|
||||
const check = (schema: { $id: string }, def: string, value: unknown): boolean => {
|
||||
const validate = ajv.getSchema(`${schema.$id}#/$defs/${def}`);
|
||||
assert.ok(validate, `no schema def ${def}`);
|
||||
return validate(value) as boolean;
|
||||
};
|
||||
const hexOf = (value: unknown): string => Buffer.from(encode(value)).toString("hex").toUpperCase();
|
||||
const sha = (bytes: Buffer | string): string => createHash("sha256").update(bytes).digest("hex");
|
||||
|
||||
test("vote API: every valid document validates, encodes to the vector bytes and decodes back", () => {
|
||||
for (const v of vote.valid) {
|
||||
assert.ok(check(voteSchema, v.def, v.message), v.name);
|
||||
assert.equal(hexOf(v.message), v.messagePackHex, v.name);
|
||||
assert.deepEqual(decode(Buffer.from(v.messagePackHex, "hex")), v.message, v.name);
|
||||
}
|
||||
});
|
||||
|
||||
test("vote API: every negative is refused by the schema, and its bytes are the vector's", () => {
|
||||
for (const v of vote.invalid) {
|
||||
assert.equal(check(voteSchema, v.def, v.message), false, `${v.name}: ${v.why}`);
|
||||
assert.equal(hexOf(v.message), v.messagePackHex, v.name);
|
||||
}
|
||||
});
|
||||
|
||||
test("vote API: the commitment is SHA-256 of the canonical bytes", () => {
|
||||
for (const c of vote.commitment) {
|
||||
const b = c.ballot;
|
||||
const bytes = Buffer.from(`gamecult.sealed_ballot.v0\n${b.motionId}\n${c.voterUserAccountId}\n${b.persona}\n${b.round}\n${b.candidate}\n${b.reason}\n${b.salt}`, "utf8");
|
||||
assert.equal(bytes.toString("hex").toUpperCase(), c.canonicalHex, c.name);
|
||||
assert.equal(sha(bytes), c.sha256, c.name);
|
||||
}
|
||||
});
|
||||
|
||||
test("vote API: a reveal matches its commit exactly when the ballot hashes to the commitment", () => {
|
||||
for (const p of vote.pairs) {
|
||||
const b = p.reveal.ballot;
|
||||
const digest = sha(Buffer.from(`gamecult.sealed_ballot.v0\n${b.motionId}\n${p.voterUserAccountId}\n${b.persona}\n${b.round}\n${b.candidate}\n${b.reason}\n${b.salt}`, "utf8"));
|
||||
assert.equal(digest === p.commit.commitment, p.matches, p.name);
|
||||
assert.ok(check(voteSchema, "cast", p.commit) && check(voteSchema, "cast", p.reveal), p.name);
|
||||
}
|
||||
});
|
||||
|
||||
test("vote API: each refusal code has a vector, a status and the vector bytes", () => {
|
||||
const codes = (voteSchema.$defs.refusal.properties.code as { enum: string[] }).enum;
|
||||
assert.deepEqual(vote.refusals.map((r: { message: { code: string } }) => r.message.code).sort(), [...codes].sort());
|
||||
for (const r of vote.refusals) {
|
||||
assert.ok(check(voteSchema, "refusal", r.message), r.name);
|
||||
assert.equal(hexOf(r.message), r.messagePackHex, r.name);
|
||||
assert.ok(r.httpStatus >= 400, r.name);
|
||||
}
|
||||
assert.ok(vote.refusals.find((r: { name: string }) => r.name === "rate_limited").message.retryAfterSeconds >= 1);
|
||||
});
|
||||
|
||||
// ---- fork card (rule source: /opt/commons/handoff/fork-desk-answer-binding-20261110.md) ----
|
||||
const jstr = (s: string): string => {
|
||||
let out = '"';
|
||||
for (const ch of s) {
|
||||
const o = ch.codePointAt(0)!;
|
||||
out += ch === '"' ? '\\"' : ch === "\\" ? "\\\\" : o === 8 ? "\\b" : o === 9 ? "\\t" : o === 10 ? "\\n" : o === 12 ? "\\f" : o === 13 ? "\\r"
|
||||
: o < 0x20 ? "\\u" + o.toString(16).padStart(4, "0") : ch;
|
||||
}
|
||||
return out + '"';
|
||||
};
|
||||
type Option = { id: string; text: string };
|
||||
type Shown = { title: string; context: string; options: Option[]; recommendation: string; depends: string[] };
|
||||
type Card = Shown & { fork_id: string; leans: string | null; deciders: string[]; status: string; ruling: string | null; ruled_from: string[]; options_hash: string };
|
||||
// RFC 8785 over the five shown members, every string NFC-normalised first; keys sorted; no numbers in the member set.
|
||||
const nfc = (s: string): string => s.normalize("NFC");
|
||||
const canonical = (c: Shown): Buffer =>
|
||||
Buffer.from(`{"context":${jstr(nfc(c.context))},"depends":[${c.depends.map((d) => jstr(nfc(d))).join(",")}],"options":[${c.options.map((o) => `{"id":${jstr(nfc(o.id))},"text":${jstr(nfc(o.text))}}`).join(",")}],"recommendation":${jstr(nfc(c.recommendation))},"title":${jstr(nfc(c.title))}}`, "utf8");
|
||||
const optionsHash = (c: Shown): string => sha(canonical(c));
|
||||
|
||||
const cardRuleErrors = (c: Card): string[] => {
|
||||
const ids = c.options.map((o) => o.id);
|
||||
const errors: string[] = [];
|
||||
if (new Set(ids).size !== ids.length) errors.push("duplicate option ids");
|
||||
if (!ids.includes(c.recommendation)) errors.push("recommendation is not an option");
|
||||
if (c.leans !== null && !ids.includes(c.leans)) errors.push("leans is not an option");
|
||||
if (new Set(c.deciders).size !== c.deciders.length) errors.push("duplicate deciders");
|
||||
if (c.status === "closed" && c.ruling === null) errors.push("closed without a ruling");
|
||||
if (c.options_hash !== optionsHash(c)) errors.push("options_hash is not the hash of the shown members");
|
||||
return errors;
|
||||
};
|
||||
|
||||
test("fork card: options hash is SHA-256 of the canonical JSON, computed here, and every shown member moves it", () => {
|
||||
for (const h of fork.optionsHash) {
|
||||
const bytes = canonical(h);
|
||||
assert.equal(bytes.toString("utf8"), h.canonical, h.name);
|
||||
assert.equal(bytes.toString("hex").toUpperCase(), h.canonicalHex, h.name);
|
||||
assert.equal(sha(bytes), h.sha256, h.name);
|
||||
}
|
||||
const byName = new Map<string, string>(fork.optionsHash.map((h: { name: string; sha256: string }) => [h.name, h.sha256]));
|
||||
const base = byName.get("heimdall_A_five_members")!;
|
||||
assert.equal(byName.get("hidden_members_do_not_move_it"), base);
|
||||
assert.equal(byName.get("heimdall_E_decomposed_equals_D"), byName.get("heimdall_D_nfc_escapes_astral"), "an NFD and an NFC spelling hash alike");
|
||||
for (const moved of ["heimdall_B_recommendation_changed", "heimdall_C_option_order_changed", "heimdall_F_option_id_changed", "heimdall_G_depends_emptied", "option_text_changed",
|
||||
"title_changed", "context_changed", "depends_order_changed", "depends_order_changed_reversed"]) {
|
||||
assert.notEqual(byName.get(moved), base, moved);
|
||||
}
|
||||
});
|
||||
|
||||
test("fork card: mutating any shown member, an option id included, changes the hash", () => {
|
||||
const card = fork.card.valid[0].message as Card;
|
||||
const base = optionsHash(card);
|
||||
const mutations: ((c: Card) => void)[] = [
|
||||
(c) => { c.title += "x"; }, (c) => { c.context += "x"; }, (c) => { c.options[0].id += "x"; }, (c) => { c.options[0].text += "x"; },
|
||||
(c) => { c.options.reverse(); }, (c) => { c.recommendation = c.options[0].id; }, (c) => { c.depends = [...c.depends, "a:b:c"]; }, (c) => { c.depends = []; },
|
||||
];
|
||||
for (const [i, mutate] of mutations.entries()) {
|
||||
const copy = structuredClone(card);
|
||||
mutate(copy);
|
||||
assert.notEqual(optionsHash(copy), base, `mutation ${i}`);
|
||||
}
|
||||
const hidden = structuredClone(card);
|
||||
hidden.fork_id = "other:question:id"; hidden.leans = "yes"; hidden.deciders = ["x"]; hidden.status = "closed"; hidden.ruled_from = ["n1"];
|
||||
assert.equal(optionsHash(hidden), base, "fork_id and the unshown members stay outside the hash");
|
||||
});
|
||||
|
||||
test("fork card: documents validate and encode to the vector bytes", () => {
|
||||
for (const v of fork.card.valid) {
|
||||
assert.ok(check(forkSchema, "card", v.message), v.name);
|
||||
assert.deepEqual(cardRuleErrors(v.message), [], v.name);
|
||||
assert.equal(hexOf(v.message), v.messagePackHex, v.name);
|
||||
}
|
||||
for (const [section, def] of [["request", "answerRequest"], ["deskAnswer", "deskAnswer"]] as const) {
|
||||
for (const v of fork[section].valid) {
|
||||
assert.ok(check(forkSchema, def, v.message), v.name);
|
||||
assert.equal(hexOf(v.message), v.messagePackHex, v.name);
|
||||
assert.deepEqual(decode(Buffer.from(v.messagePackHex, "hex")), v.message, v.name);
|
||||
}
|
||||
}
|
||||
const resultCodes = (forkSchema.$defs.deskAnswerRefused.properties.code as { enum: string[] }).enum;
|
||||
assert.deepEqual(fork.results.map((r: { name: string }) => r.name).sort(), [...resultCodes].sort());
|
||||
for (const v of fork.results) {
|
||||
assert.ok(check(forkSchema, "answerResult", v.message), v.name);
|
||||
assert.equal(hexOf(v.message), v.messagePackHex, v.name);
|
||||
assert.equal(v.message.card !== null, v.name === "stale_fork", "only a stale refusal shows the new fork");
|
||||
}
|
||||
const doorCodes = (forkSchema.$defs.refusal.properties.code as { enum: string[] }).enum;
|
||||
assert.deepEqual(fork.doorRefusals.map((r: { name: string }) => r.name).sort(), [...doorCodes].sort());
|
||||
for (const v of fork.doorRefusals) {
|
||||
assert.ok(check(forkSchema, "refusal", v.message), v.name);
|
||||
assert.equal(hexOf(v.message), v.messagePackHex, v.name);
|
||||
}
|
||||
});
|
||||
|
||||
test("fork card: every negative is refused by the schema or a stated rule", () => {
|
||||
for (const v of fork.card.invalid) {
|
||||
assert.ok(!check(forkSchema, "card", v.message) || cardRuleErrors(v.message).length > 0, `${v.name}: ${v.why}`);
|
||||
}
|
||||
for (const [section, def] of [["request", "answerRequest"], ["deskAnswer", "deskAnswer"]] as const) {
|
||||
for (const v of fork[section].invalid) {
|
||||
assert.equal(check(forkSchema, def, v.message), false, `${v.name}: ${v.why}`);
|
||||
assert.equal(hexOf(v.message), v.messagePackHex, v.name);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
const TAILS: Record<string, string> = {
|
||||
unauthenticated: "a verified session is required",
|
||||
body_forbidden_field: "answerer and authority are set by the receiver",
|
||||
body_unknown_field: "the body is a closed shape",
|
||||
body_invalid: "the body does not satisfy gamecult.fork.answer_request.v0",
|
||||
fork_unknown: "no such fork",
|
||||
};
|
||||
const REQUEST_KEYS = ["schemaVersion", "fork_id", "options_hash", "answer", "retracted", "retracts", "answered_at"];
|
||||
const hasKey = (v: unknown, names: string[]): boolean =>
|
||||
typeof v === "object" && v !== null && Object.entries(v).some(([k, x]) => names.includes(k) || hasKey(x, names));
|
||||
const SECONDS = (iso: string): number => Date.parse(iso);
|
||||
|
||||
// The reference judge: Huginn's half behind the second door as fork-card.md states it. Server-set fields come from the step.
|
||||
function judge(start: Card, steps: { kind: string; body: Record<string, any> | null; server: Record<string, any> | null; card?: Card }[]) {
|
||||
let card = start;
|
||||
const answers: Record<string, any>[] = [];
|
||||
const responses: unknown[] = [];
|
||||
const counted = (): string | null => {
|
||||
const hash = optionsHash(card);
|
||||
const live = answers.find((a) => (a.outcome === "Counted" || a.outcome === "Revision") && a.options_hash === hash
|
||||
&& !answers.some((r) => r.outcome === "Retraction" && r.retracts === a.answer_id));
|
||||
return live ? live.answer_id : null;
|
||||
};
|
||||
for (const step of steps) {
|
||||
if (step.kind === "matrix_event") { responses.push(null); continue; } // rule 9: Matrix never changes a decision
|
||||
if (step.kind === "card") { card = step.card!; responses.push(null); continue; }
|
||||
const s = step.server!, b = step.body!;
|
||||
const door = (code: string) => ({ schemaVersion: "gamecult.fork.refusal.v0", code, error: `${code}: ${TAILS[code]}` });
|
||||
const store = (outcome: string, link: string | null, code: string | null) => {
|
||||
const a = b.answer ?? { choice: null, note: null };
|
||||
const doc = { schemaVersion: "gamecult.desk_answer.v0", answer_id: s.answer_id, fork_id: b.fork_id, outcome,
|
||||
of: outcome === "Revision" ? link : null, retracts: outcome === "Retraction" ? link : null, code: outcome === "Refused" ? code : null,
|
||||
options_hash: b.options_hash, choice: a.choice, note: a.note, answered_at: b.answered_at, received_at: s.received_at,
|
||||
claim_in_future: SECONDS(b.answered_at) - SECONDS(s.received_at) > 300_000, answerer: s.answerer, authority: s.authority };
|
||||
answers.push(doc);
|
||||
return { schemaVersion: "gamecult.fork.answer_result.v0", answer: doc, card: code === "stale_fork" ? card : null };
|
||||
};
|
||||
const refuse = (code: string) => store("Refused", null, code);
|
||||
let response: unknown;
|
||||
if (s.answerer === null) response = door("unauthenticated");
|
||||
else if (hasKey(b, ["answerer", "authority"])) response = door("body_forbidden_field");
|
||||
else if (Object.keys(b).some((k) => !REQUEST_KEYS.includes(k)) || (b.answer && Object.keys(b.answer).some((k) => k !== "choice" && k !== "note"))) response = door("body_unknown_field");
|
||||
else if (!check(forkSchema, "answerRequest", b) || (!b.retracted && b.answer === null)) response = door("body_invalid");
|
||||
else if (b.fork_id !== card.fork_id) response = door("fork_unknown");
|
||||
else if (b.options_hash !== optionsHash(card)) response = refuse("stale_fork");
|
||||
else if (!card.deciders.includes(s.answerer)) response = refuse("not_a_decider");
|
||||
else if (card.status === "closed") response = refuse("fork_closed");
|
||||
else if (b.retracted || b.retracts !== null) {
|
||||
const target = answers.find((a) => a.answer_id === b.retracts && (a.outcome === "Counted" || a.outcome === "Revision") && a.options_hash === b.options_hash);
|
||||
if (!b.retracted || b.answer !== null || b.retracts === null || !target) response = refuse("bad_retraction");
|
||||
else if (answers.some((a) => a.outcome === "Retraction" && a.retracts === target.answer_id)) response = refuse("already_retracted");
|
||||
else if (card.ruled_from.includes(target.answer_id)) response = refuse("ruled");
|
||||
else response = store("Retraction", target.answer_id, null);
|
||||
} else if (b.answer.choice !== null && !card.options.some((o) => o.id === b.answer.choice)) response = refuse("choice_invalid");
|
||||
else {
|
||||
const first = counted();
|
||||
response = first === null ? store("Counted", null, null) : store("Revision", first, null);
|
||||
}
|
||||
responses.push(response);
|
||||
}
|
||||
return { responses, record: { schemaVersion: "gamecult.fork.record.v0", fork_id: card.fork_id, options_hash: optionsHash(card), counted_answer_id: counted(), answers } };
|
||||
}
|
||||
|
||||
test("fork card: every scenario replays to the vector's responses and record, bytes included", () => {
|
||||
for (const sc of fork.scenarios) {
|
||||
assert.equal(optionsHash(sc.card), sc.optionsHash, sc.name);
|
||||
assert.deepEqual(cardRuleErrors(sc.card), [], sc.name);
|
||||
const { responses, record } = judge(sc.card, sc.steps);
|
||||
assert.deepEqual(responses, sc.steps.map((s: { response: unknown }) => s.response), sc.name);
|
||||
assert.deepEqual(record, sc.record, sc.name);
|
||||
assert.ok(check(forkSchema, "record", record), sc.name);
|
||||
assert.equal(hexOf(record), sc.recordHex, sc.name);
|
||||
for (const s of sc.steps) if (s.response) assert.ok(check(forkSchema, "answerResult", s.response) || check(forkSchema, "refusal", s.response), sc.name);
|
||||
}
|
||||
});
|
||||
|
||||
test("fork card: a body refused for its shape reaches Huginn never, so the record stores nothing for it", () => {
|
||||
const door = new Set(["unauthenticated", "body_forbidden_field", "body_unknown_field", "body_invalid", "fork_unknown"]);
|
||||
let doorRefusals = 0;
|
||||
for (const sc of fork.scenarios) {
|
||||
for (const s of sc.steps) {
|
||||
if (s.response && door.has(s.response.code)) {
|
||||
doorRefusals++;
|
||||
assert.ok(!sc.record.answers.some((a: { answer_id: string }) => a.answer_id === s.server.answer_id), `${sc.name} stored a door refusal`);
|
||||
}
|
||||
}
|
||||
}
|
||||
assert.ok(doorRefusals >= 8);
|
||||
});
|
||||
|
||||
test("fork card: the scenarios cover every outcome and every refusal code", () => {
|
||||
const seen = new Set<string>();
|
||||
for (const sc of fork.scenarios) {
|
||||
for (const s of sc.steps) if (s.response) seen.add(s.response.code ?? s.response.answer.code ?? s.response.answer.outcome);
|
||||
for (const a of sc.record.answers) seen.add(a.outcome);
|
||||
}
|
||||
for (const want of ["Counted", "Revision", "Retraction", "Refused", "unauthenticated", "body_forbidden_field", "body_unknown_field", "body_invalid", "fork_unknown", "stale_fork", "not_a_decider",
|
||||
"fork_closed", "choice_invalid", "bad_retraction", "already_retracted", "ruled"]) {
|
||||
assert.ok(seen.has(want), want);
|
||||
}
|
||||
});
|
||||
|
||||
// A ruling may carry authority Operator only if it cites a Counted desk_answer stamped Operator (binding file 1.3d).
|
||||
const admitRuling = (cite: { authority: string; answer_id: string | null }, answers: { answer_id: string; outcome: string; authority: string }[]): boolean => {
|
||||
if (cite.authority !== "Operator") return true;
|
||||
const cited = answers.find((a) => a.answer_id === cite.answer_id);
|
||||
return cited !== undefined && cited.outcome === "Counted" && cited.authority === "Operator";
|
||||
};
|
||||
|
||||
test("fork card: operator authority needs a cited Counted answer record", () => {
|
||||
for (const c of fork.rulingCites) {
|
||||
assert.ok(check(forkSchema, "rulingCite", c.message), c.name);
|
||||
assert.equal(hexOf(c.message), c.messagePackHex, c.name);
|
||||
assert.equal(admitRuling(c.message, c.answers), c.admit, c.name);
|
||||
}
|
||||
});
|
||||
378
scripts/gen-governance-vectors.py
Normal file
378
scripts/gen-governance-vectors.py
Normal file
|
|
@ -0,0 +1,378 @@
|
|||
#!/usr/bin/env python3
|
||||
"""Writes contracts/cultnet/governance/{vote-api,fork-card}-vectors.json.
|
||||
|
||||
The bytes are spec-authored with msgpack-python (minimal ints, str8 only past 31 bytes, map keys in the order written here);
|
||||
each runtime's conformance test re-encodes the `message` and must produce the same hex. Run: python3 scripts/gen-governance-vectors.py
|
||||
Needs: pip install msgpack.
|
||||
"""
|
||||
import hashlib, json, pathlib, unicodedata
|
||||
import msgpack
|
||||
|
||||
OUT = pathlib.Path(__file__).resolve().parent.parent / "contracts" / "cultnet" / "governance"
|
||||
def hx(m): return msgpack.packb(m, use_bin_type=True).hex().upper()
|
||||
def sha(b): return hashlib.sha256(b).hexdigest()
|
||||
|
||||
M1 = "5b1c2e9a-3f4d-4c8e-9a61-0d7e2f4b8c13"
|
||||
M2 = "a7d3f0c2-6b15-4e29-8f3a-91c4d5e6f708"
|
||||
V1 = "0c9f6a4e-7d2b-4a35-b8e1-3f5a6c7d8e90"
|
||||
T0 = "2026-11-12T18:00:00Z"; T1 = "2026-11-15T18:00:00Z"; T2 = "2026-11-18T18:00:00Z"
|
||||
SALT = "d41d8cd98f00b204e9800998ecf8427e5f1c0a7b3e92d6a48b15c7f0e3a9d2b6"
|
||||
|
||||
# ---------- vote API ----------
|
||||
def commitment_bytes(motion, voter, persona, rnd, candidate, reason, salt):
|
||||
return f"gamecult.sealed_ballot.v0\n{motion}\n{voter}\n{persona}\n{rnd}\n{candidate}\n{reason}\n{salt}".encode("utf-8")
|
||||
|
||||
def ballot(motion=M1, rnd=1, candidate="Adopt the ledger", reason="It keeps the books honest.", salt=SALT, persona=""):
|
||||
return {"schema": "gamecult.sealed_ballot.v0", "motionId": motion, "round": rnd, "candidate": candidate,
|
||||
"reason": reason, "salt": salt, "persona": persona}
|
||||
def digest(b, voter=V1):
|
||||
return sha(commitment_bytes(b["motionId"], voter, b["persona"], b["round"], b["candidate"], b["reason"], b["salt"]))
|
||||
|
||||
B_MEMBER = ballot()
|
||||
B_PERSONA = ballot(persona="libby", candidate="Defer to round two", reason="The sealed window is too short to weigh both.")
|
||||
C_MEMBER = digest(B_MEMBER); C_PERSONA = digest(B_PERSONA)
|
||||
|
||||
def receipt(commitment, revision=1, revealed=False, motion=M1):
|
||||
return {"schemaVersion": "gamecult.vote.receipt.v0", "motionId": motion, "voterUserAccountId": V1,
|
||||
"commitment": commitment, "revision": revision, "committedAtUtc": "2026-11-12T19:04:11.1234567Z", "revealed": revealed}
|
||||
def motion(phase, your=None, mid=M1):
|
||||
return {"schemaVersion": "gamecult.vote.motion.v0", "id": mid, "title": "Ledger or lantern", "summary": "Pick one.",
|
||||
"round": 1, "priorMotionId": None, "ballotSchema": "gamecult.sealed_ballot.v0", "phase": phase,
|
||||
"candidates": ["Adopt the ledger", "Defer to round two"], "commitClosesAtUtc": T0, "closesAtUtc": T1,
|
||||
"yourVoterId": V1, "yourCommitment": your, "announcement": None}
|
||||
def cast_commit(c, motion=M1): return {"schemaVersion": "gamecult.vote.cast.v0", "motionId": motion, "verb": "commit", "commitment": c, "ballot": None}
|
||||
def cast_reveal(b): return {"schemaVersion": "gamecult.vote.cast.v0", "motionId": b["motionId"], "verb": "reveal", "commitment": None, "ballot": b}
|
||||
def refusal(code, error, retry=None): return {"schemaVersion": "gamecult.vote.refusal.v0", "code": code, "error": error, "retryAfterSeconds": retry}
|
||||
def item(i, title, phase): return {"id": i, "title": title, "round": 1, "phase": phase, "commitClosesAtUtc": T0, "closesAtUtc": T1}
|
||||
|
||||
valid = [
|
||||
("motion_list_request_all", "motionListRequest", {"schemaVersion": "gamecult.vote.motion_list_request.v0", "phase": None}),
|
||||
("motion_list_request_reveal", "motionListRequest", {"schemaVersion": "gamecult.vote.motion_list_request.v0", "phase": "Reveal"}),
|
||||
("motion_list_two", "motionList", {"schemaVersion": "gamecult.vote.motion_list.v0", "motions": [item(M1, "Ledger or lantern", "Commit"), item(M2, "Second thoughts", "Closed")]}),
|
||||
("motion_list_empty", "motionList", {"schemaVersion": "gamecult.vote.motion_list.v0", "motions": []}),
|
||||
("motion_read_request", "motionReadRequest", {"schemaVersion": "gamecult.vote.motion_read_request.v0", "motionId": M1}),
|
||||
("motion_commit_phase_no_receipt", "motion", motion("Commit")),
|
||||
("motion_reveal_phase_committed", "motion", motion("Reveal", receipt(C_MEMBER))),
|
||||
("motion_closed_revealed", "motion", motion("Closed", receipt(C_MEMBER, 2, True))),
|
||||
("cast_commit", "cast", cast_commit(C_MEMBER)),
|
||||
("cast_reveal_member", "cast", cast_reveal(B_MEMBER)),
|
||||
("cast_reveal_persona", "cast", cast_reveal(B_PERSONA)),
|
||||
("receipt_committed", "receipt", receipt(C_MEMBER)),
|
||||
("receipt_revealed_echoes_no_ballot", "receipt", receipt(C_MEMBER, 2, True)),
|
||||
]
|
||||
refusals = [
|
||||
("unauthenticated", 401, "no bearer, or one Heimdall does not know", refusal("unauthenticated", "A bearer token is required.")),
|
||||
("forbidden", 403, "authenticated, not an Active member, or a Mind key used on a motion route it may not call", refusal("forbidden", "Only Active members may vote.")),
|
||||
("not_found", 404, "unknown motion id, a standard (non-sealed) motion id, or reveal without a prior commit", refusal("not_found", "No such sealed-ballot motion.")),
|
||||
("wrong_phase", 409, "commit outside Commit, reveal outside Reveal", refusal("wrong_phase", "The commit window is not open.")),
|
||||
("contended", 409, "reveal raced the close fence; send it again", refusal("contended", "The reveal is contended; send it again.")),
|
||||
("invalid", 400, "body fails vote-api.schema.json or an in-document rule (untrimmed candidate or reason, candidate not in the motion, round or motionId mismatch, persona wrong for the credential)", refusal("invalid", "The cast does not satisfy gamecult.vote.cast.v0.")),
|
||||
("commitment_mismatch", 400, "reveal whose canonical bytes do not hash to the stored commitment", refusal("commitment_mismatch", "The ballot does not match the stored commitment.")),
|
||||
("payload_too_large", 413, "body over 4096 bytes", refusal("payload_too_large", "The body exceeds 4096 bytes.")),
|
||||
("rate_limited", 429, "more than the per-credential cast rate; Retry-After mirrors retryAfterSeconds", refusal("rate_limited", "Too many casts.", 30)),
|
||||
]
|
||||
def tweak(d, **kw):
|
||||
d = json.loads(json.dumps(d));
|
||||
for k, v in kw.items():
|
||||
if k.startswith("b_"): d["ballot"][k[2:]] = v
|
||||
else: d[k] = v
|
||||
return d
|
||||
rev = cast_reveal(B_MEMBER)
|
||||
invalid = [
|
||||
("cast_commit_carries_a_ballot", "cast", tweak(cast_commit(C_MEMBER), ballot=B_MEMBER), "a commit names exactly the commitment; a ballot beside it would put content on the wire before the reveal"),
|
||||
("cast_reveal_carries_a_commitment", "cast", tweak(rev, commitment=C_MEMBER), "a reveal names exactly the ballot"),
|
||||
("cast_commit_without_commitment", "cast", tweak(cast_commit(C_MEMBER), commitment=None), "commit needs its hash"),
|
||||
("cast_verb_unknown", "cast", tweak(rev, verb="recast"), "verb is commit or reveal"),
|
||||
("cast_commitment_uppercase", "cast", cast_commit(C_MEMBER.upper()), "commitment is lowercase hex"),
|
||||
("cast_commitment_63_chars", "cast", cast_commit(C_MEMBER[:63]), "commitment is 64 hex characters"),
|
||||
("cast_unknown_key", "cast", tweak(cast_commit(C_MEMBER), note="hi"), "closed document"),
|
||||
("cast_motion_id_uppercase", "cast", cast_commit(C_MEMBER, M1.upper()), "ids are lowercase D-format uuids"),
|
||||
("ballot_reason_empty", "cast", tweak(rev, b_reason=""), "reason is 1 to 280 characters"),
|
||||
("ballot_reason_281", "cast", tweak(rev, b_reason="x" * 281), "reason is at most 280 characters"),
|
||||
("ballot_reason_multiline", "cast", tweak(rev, b_reason="one\ntwo"), "reason is a single line"),
|
||||
("ballot_reason_leading_space", "cast", tweak(rev, b_reason=" because"), "the contract refuses untrimmed text; it never trims for the sender, so the hashed bytes are the sent bytes"),
|
||||
("ballot_candidate_trailing_nbsp", "cast", tweak(rev, b_candidate="Adopt the ledger "), "NO-BREAK SPACE is White_Space; trimming is judged by the same list as write-receipt Required fields"),
|
||||
("ballot_salt_31_hex", "cast", tweak(rev, b_salt=SALT[:31]), "salt is 32 to 128 lowercase hex characters"),
|
||||
("ballot_salt_uppercase", "cast", tweak(rev, b_salt=SALT.upper()), "salt is lowercase hex"),
|
||||
("ballot_schema_wrong", "cast", tweak(rev, b_schema="gamecult.sealed_ballot.v1"), "ballot schema is gamecult.sealed_ballot.v0"),
|
||||
("ballot_persona_with_space", "cast", tweak(rev, b_persona="my mind"), "persona is printable ASCII without space, at most 64"),
|
||||
("ballot_round_zero", "cast", tweak(rev, b_round=0), "rounds start at 1"),
|
||||
("receipt_echoes_candidate", "receipt", dict(receipt(C_MEMBER, 2, True), candidate="Adopt the ledger"), "sealed casts never echo ballot content; the receipt is closed"),
|
||||
("receipt_echoes_salt", "receipt", dict(receipt(C_MEMBER, 2, True), salt=SALT), "the salt never comes back before close"),
|
||||
("receipt_revision_zero", "receipt", receipt(C_MEMBER, 0), "revision starts at 1"),
|
||||
("receipt_time_without_zone", "receipt", dict(receipt(C_MEMBER), committedAtUtc="2026-11-12T19:04:11"), "instants are UTC with a Z"),
|
||||
("motion_one_candidate", "motion", dict(motion("Commit"), candidates=["Only"]), "a motion has two or more candidates"),
|
||||
("motion_phase_unknown", "motion", dict(motion("Commit"), phase="Open"), "phase is Commit, Reveal or Closed"),
|
||||
("motion_leaks_tally", "motion", dict(motion("Reveal"), tally=[]), "mid-vote a client sees no tallies; the document is closed"),
|
||||
("motion_list_item_without_phase", "motionList", {"schemaVersion": "gamecult.vote.motion_list.v0", "motions": [{k: v for k, v in item(M1, "x", "Commit").items() if k != "phase"}]}, "every item names its phase"),
|
||||
("refusal_code_unknown", "refusal", refusal("teapot", "no"), "the code set is closed"),
|
||||
("refusal_without_error", "refusal", dict(refusal("invalid", "x"), error=""), "error is non-empty"),
|
||||
]
|
||||
pairs = [
|
||||
{"name": "member_commit_matches_reveal", "voterUserAccountId": V1, "commit": cast_commit(C_MEMBER), "reveal": cast_reveal(B_MEMBER), "matches": True},
|
||||
{"name": "persona_commit_matches_reveal", "voterUserAccountId": V1, "commit": cast_commit(C_PERSONA), "reveal": cast_reveal(B_PERSONA), "matches": True},
|
||||
{"name": "reveal_with_changed_reason_does_not_match", "voterUserAccountId": V1, "commit": cast_commit(C_MEMBER), "reveal": cast_reveal(ballot(reason="It keeps the books honest!")), "matches": False, "refusal": "commitment_mismatch"},
|
||||
{"name": "reveal_under_another_voter_does_not_match", "voterUserAccountId": M2, "commit": cast_commit(C_MEMBER), "reveal": cast_reveal(B_MEMBER), "matches": False, "refusal": "commitment_mismatch"},
|
||||
{"name": "persona_dropped_does_not_match", "voterUserAccountId": V1, "commit": cast_commit(C_PERSONA), "reveal": cast_reveal(ballot(persona="", candidate=B_PERSONA["candidate"], reason=B_PERSONA["reason"])), "matches": False, "refusal": "commitment_mismatch"},
|
||||
]
|
||||
commitments = []
|
||||
for name, b, voter in [("member", B_MEMBER, V1), ("persona", B_PERSONA, V1), ("non_ascii", ballot(candidate="Café ☕", reason="naïve – but fine"), V1),
|
||||
("decomposed_e_is_another_ballot", ballot(candidate="Café ☕", reason="naïve – but fine"), V1)]:
|
||||
cb = commitment_bytes(b["motionId"], voter, b["persona"], b["round"], b["candidate"], b["reason"], b["salt"])
|
||||
commitments.append({"name": name, "voterUserAccountId": voter, "ballot": b, "canonicalHex": cb.hex().upper(), "sha256": sha(cb)})
|
||||
|
||||
vote = {
|
||||
"provenance": "Spec-authored with msgpack-python (scripts/gen-governance-vectors.py); every runtime re-encodes `message` and must match messagePackHex. Commitment hashes follow vote-api.md 'Commitment'.",
|
||||
"commitment": commitments,
|
||||
"valid": [{"name": n, "def": d, "message": m, "messagePackHex": hx(m)} for n, d, m in valid],
|
||||
"invalid": [{"name": n, "def": d, "message": m, "why": w, "messagePackHex": hx(m)} for n, d, m, w in invalid],
|
||||
"pairs": pairs,
|
||||
"refusals": [{"name": n, "httpStatus": s, "trigger": t, "message": m, "messagePackHex": hx(m)} for n, s, t, m in refusals],
|
||||
}
|
||||
(OUT / "vote-api-vectors.json").write_text(json.dumps(vote, indent=2, ensure_ascii=False) + "\n", encoding="utf-8")
|
||||
|
||||
# ---------- fork card ----------
|
||||
# Rule source: /opt/commons/handoff/fork-desk-answer-binding-20261110.md (Heimdall), sections 1.1 to 1.5 and 1.3a. The seven hash
|
||||
# vectors A to G below are that file's, with its expected digests asserted.
|
||||
def jstr(x):
|
||||
out = ['"']
|
||||
for ch in x:
|
||||
o = ord(ch)
|
||||
if ch == '"': out.append('\\"')
|
||||
elif ch == "\\": out.append("\\\\")
|
||||
elif ch == "\b": out.append("\\b")
|
||||
elif ch == "\t": out.append("\\t")
|
||||
elif ch == "\n": out.append("\\n")
|
||||
elif ch == "\f": out.append("\\f")
|
||||
elif ch == "\r": out.append("\\r")
|
||||
elif o < 0x20: out.append("\\u%04x" % o)
|
||||
else: out.append(ch)
|
||||
out.append('"')
|
||||
return "".join(out)
|
||||
def nfc(x): return unicodedata.normalize("NFC", x)
|
||||
def canonical_json(title, context, options, recommendation, depends):
|
||||
"""RFC 8785 over {context, depends, options:[{id,text}], recommendation, title} after NFC on every string; keys sorted; no numbers. (None is accepted only to check Heimdall's null digests.)"""
|
||||
opts = ",".join('{"id":%s,"text":%s}' % (jstr(nfc(o["id"])), jstr(nfc(o["text"]))) for o in options)
|
||||
dep = ",".join(jstr(nfc(d)) for d in depends)
|
||||
rec = "null" if recommendation is None else jstr(nfc(recommendation))
|
||||
return ('{"context":%s,"depends":[%s],"options":[%s],"recommendation":%s,"title":%s}' % (jstr(nfc(context)), dep, opts, rec, jstr(nfc(title)))).encode("utf-8")
|
||||
|
||||
OP = "e0d1c2b3-a495-4687-8879-6a5b4c3d2e1f"; ST = "11111111-2222-4333-8444-555555555555"; HM = "9f8e7d6c-5b4a-4392-8170-0f1e2d3c4b5a"
|
||||
FID = "gamecult-chat:question:slot-mark-consent"
|
||||
OPT = [{"id": "yes", "text": "Share it."}, {"id": "no", "text": "Keep it local."}]
|
||||
DEP = ["gamecult-chat:question:file-zone-audience"]
|
||||
def chash(c): return sha(canonical_json(c["title"], c["context"], c["options"], c["recommendation"], c["depends"]))
|
||||
def card(**kw):
|
||||
c = {"schemaVersion": "gamecult.fork.card.v0", "fork_id": FID, "title": "Slot mark consent", "context": "Should a slot mark be shared?",
|
||||
"options": OPT, "recommendation": "no", "depends": DEP, "leans": None, "flag": None,
|
||||
"raised_in": {"kind": "CutSpec", "id": "cultlib-gaps:cut_spec:cut-slot-tolerance.r4"}, "deciders": [OP],
|
||||
"status": "open", "ruling": None, "ruled_from": [], "order": 3, "options_hash": ""}
|
||||
c.update(kw); c["options_hash"] = chash(c); return c
|
||||
CARD = card(); H = CARD["options_hash"]
|
||||
|
||||
hashes = []
|
||||
CLAIMED = {"A": "7d95fc87d532420611fe415c1def27a3447617bb68235f46e3af130aea361c48", "B": "14b8ffbdb61106dbc06f69f3fc8e2072af7a205d766e4cc040e89733013d54dc",
|
||||
"C": "ac67b41539f869f6203ac0908819df046e4328b27795743a919bd4ab390596b5", "D": "71d5c413478ad06aaa1cbdd9e91c53f0cb4cef1dbd4c51bf3d473bccc6a5a272",
|
||||
"E": "71d5c413478ad06aaa1cbdd9e91c53f0cb4cef1dbd4c51bf3d473bccc6a5a272", "F": "fb53aa3a9f7e74029f755d774064eedd240ab90d8fc64723f97c31b3d2846d40",
|
||||
"G": "f856edce121db4121b5f1af400b1c471ebbfa6579a042aca9a34ae31c15f5c86"}
|
||||
def hv(name, c, heimdall=None):
|
||||
b = canonical_json(c["title"], c["context"], c["options"], c["recommendation"], c["depends"])
|
||||
if heimdall: print("heimdall", heimdall, "same" if CLAIMED[heimdall] == sha(b) else f"DIFFERS mine={sha(b)} his={CLAIMED[heimdall]}")
|
||||
hashes.append({"name": name, "heimdall": heimdall, "title": c["title"], "context": c["context"], "options": c["options"],
|
||||
"recommendation": c["recommendation"], "depends": c["depends"], "canonical": b.decode("utf-8"),
|
||||
"canonicalHex": b.hex().upper(), "sha256": sha(b)})
|
||||
CD = dict(title="Caf\u00e9 \u201cdesk\u201d", context="Line one\nLine two \u2014 \U0001F600", options=[{"id": "a", "text": "x"}, {"id": "b", "text": "y"}], recommendation="a", depends=[])
|
||||
hv("heimdall_A_five_members", CARD, "A")
|
||||
hv("heimdall_B_recommendation_changed", card(recommendation="yes"), "B")
|
||||
hv("heimdall_C_option_order_changed", card(options=[OPT[1], OPT[0]]), "C")
|
||||
hv("heimdall_D_nfc_escapes_astral", card(**CD), "D")
|
||||
hv("heimdall_E_decomposed_equals_D", card(**dict(CD, title="Cafe\u0301 \u201cdesk\u201d")), "E")
|
||||
hv("heimdall_F_option_id_changed", card(options=[{"id": "share", "text": "Share it."}, OPT[1]]), "F")
|
||||
hv("heimdall_G_depends_emptied", card(depends=[]), "G")
|
||||
hv("option_text_changed", card(options=[dict(OPT[0], text="Share it!"), OPT[1]]))
|
||||
hv("title_changed", card(title="Slot mark consent?"))
|
||||
hv("context_changed", card(context="Should slot marks be shared?"))
|
||||
hv("depends_order_changed", card(depends=DEP + ["x:question:y"]))
|
||||
hv("depends_order_changed_reversed", card(depends=["x:question:y"] + DEP))
|
||||
hv("hidden_members_do_not_move_it", card(leans="yes", flag="provisional", deciders=[OP, HM], status="closed", ruling="r:1", ruled_from=["rc-1"], order=9, raised_in=None, fork_id="other:question:id"))
|
||||
hv("control_characters_and_markup_are_escaped_minimally", card(title='q " b \\ t \t n \n bell \u0007 del \u007f', context="</script> \u2028 &"))
|
||||
hv("astral_option_id_and_text", card(options=[{"id": "\U0001F600", "text": "\U0001D11E"}, OPT[1]]))
|
||||
assert [h["sha256"] for h in hashes if h["heimdall"] == "D"] == [h["sha256"] for h in hashes if h["heimdall"] == "E"]
|
||||
assert len({h["sha256"] for h in hashes if h["name"] in ("heimdall_A_five_members", "hidden_members_do_not_move_it")}) == 1
|
||||
|
||||
def body(choice=None, note=None, h=None, at="2026-11-12T19:02:00Z", fork=FID, retracts=None, answer="auto"):
|
||||
a = {"choice": choice, "note": note} if answer == "auto" else answer
|
||||
if answer == "auto" and choice is None and note is None: a = None
|
||||
return {"schemaVersion": "gamecult.fork.answer_request.v0", "fork_id": fork, "options_hash": h or H, "answer": a,
|
||||
"retracted": retracts is not None, "retracts": retracts, "answered_at": at}
|
||||
def retraction(target, **kw): return body(retracts=target, **kw)
|
||||
NOTE = "Keep it local: a slot mark is the author's call, and sharing it is a separate act."
|
||||
def srv(aid, who, at, auth="Operator"): return {"answer_id": aid, "answerer": who, "authority": auth, "received_at": at}
|
||||
TAIL = {"unauthenticated": "a verified session is required", "body_forbidden_field": "answerer and authority are set by the receiver", "body_unknown_field": "the body is a closed shape",
|
||||
"body_invalid": "the body does not satisfy gamecult.fork.answer_request.v0", "fork_unknown": "no such fork"}
|
||||
DOOR = set(TAIL)
|
||||
def refusal_doc(code): return {"schemaVersion": "gamecult.fork.refusal.v0", "code": code, "error": f"{code}: {TAIL[code]}"}
|
||||
def future(a, r):
|
||||
import datetime as dt
|
||||
p = lambda x: dt.datetime.strptime(x, "%Y-%m-%dT%H:%M:%SZ")
|
||||
return (p(a) - p(r)).total_seconds() > 300
|
||||
def da(outcome, link, code, b, server):
|
||||
a = b["answer"] or {"choice": None, "note": None}
|
||||
return {"schemaVersion": "gamecult.desk_answer.v0", "answer_id": server["answer_id"], "fork_id": b["fork_id"], "outcome": outcome,
|
||||
"of": link if outcome == "Revision" else None, "retracts": link if outcome == "Retraction" else None, "code": code if outcome == "Refused" else None,
|
||||
"options_hash": b["options_hash"], "choice": a["choice"], "note": a["note"], "answered_at": b["answered_at"], "received_at": server["received_at"],
|
||||
"claim_in_future": future(b["answered_at"], server["received_at"]), "answerer": server["answerer"], "authority": server["authority"]}
|
||||
def result(answer, c=None): return {"schemaVersion": "gamecult.fork.answer_result.v0", "answer": answer, "card": c}
|
||||
def record(counted, answers, h=H, fork=FID): return {"schemaVersion": "gamecult.fork.record.v0", "fork_id": fork, "options_hash": h, "counted_answer_id": counted, "answers": answers}
|
||||
T = lambda m, sec=5: f"2026-11-12T19:{m:02d}:{sec:02d}Z"
|
||||
def step(kind, body_=None, server=None, response=None, **kw):
|
||||
d = {"kind": kind, "body": body_, "server": server, "response": response}; d.update(kw); return d
|
||||
def ok(b, server, outcome, of):
|
||||
a = da(outcome, of, None, b, server); return step("answer", b, server, result(a)), a
|
||||
def rf(b, server, code, c=None):
|
||||
a = da("Refused", None, code, b, server); return step("answer", b, server, result(a, c)), a
|
||||
def door(b, server, code): return step("answer", b, server, refusal_doc(code)), None
|
||||
def sc(name, c, items, counted, h=None):
|
||||
steps = [i[0] if isinstance(i, tuple) else i for i in items]
|
||||
answers = [i[1] for i in items if isinstance(i, tuple) and i[1] is not None]
|
||||
return (name, c, steps, record(counted, answers, h or c["options_hash"]))
|
||||
|
||||
x1 = body("no", NOTE); sx1 = srv("n1", OP, T(2))
|
||||
x2 = body("yes", "On reflection, share it.", at="2026-11-12T19:06:00Z"); sx2 = srv("n2", OP, T(6))
|
||||
y1 = body("yes", "Share it, the audience is settled.", at="2026-11-12T19:05:00Z"); sy1 = srv("n3", HM, T(5), auth="Steward")
|
||||
z1 = body("yes", "Share, please."); sz = srv("n4", ST, T(3), auth="Member")
|
||||
pick = body("no", None); words = body(None, NOTE)
|
||||
STALE_H = chash(card(recommendation="yes"))
|
||||
def stale_body(**kw): return body("no", NOTE, h=STALE_H, **kw)
|
||||
FORKC = card(status="closed", ruling="cultlib-gaps:resolution:question.slot-mark-consent.n1")
|
||||
Y_CARD = card(deciders=[OP, HM])
|
||||
fork_edit = card(title="Slot mark consent (revised)"); H2 = fork_edit["options_hash"]
|
||||
RET = lambda t: retraction(t, at="2026-11-12T19:09:00Z")
|
||||
def sr(aid, who=OP, m=9, auth="Operator"): return srv(aid, who, T(m), auth)
|
||||
MATRIX = [step("matrix_event", None, None, None, matrix_kind=k, choice=c) for k, c in (("m.replace", "yes"), ("m.redaction", None), ("m.reaction", "yes"))]
|
||||
X1 = ok(x1, sx1, "Counted", None)
|
||||
scen = [
|
||||
sc("operator_answers_and_is_counted", CARD, [X1], "n1"),
|
||||
sc("choice_alone_is_counted", CARD, [ok(pick, sx1, "Counted", None)], "n1"),
|
||||
sc("note_alone_is_counted_a_clear_pick", CARD, [ok(words, sx1, "Counted", None)], "n1"),
|
||||
sc("V1_answerer_in_the_body_is_refused_at_the_door_and_stores_nothing", CARD, [door(dict(x1, answerer="x"), sx1, "body_forbidden_field")], None),
|
||||
sc("V1_authority_nested_in_answer_is_refused", CARD, [door(dict(x1, answer={"choice": "no", "note": None, "authority": "Operator"}), sx1, "body_forbidden_field")], None),
|
||||
sc("V1_both_are_refused_not_stripped", CARD, [door(dict(x1, answerer=OP, authority="Operator"), sx1, "body_forbidden_field")], None),
|
||||
sc("an_unknown_key_is_refused_the_body_is_closed", CARD, [door(dict(x1, mood="happy"), sx1, "body_unknown_field")], None),
|
||||
sc("an_answer_with_neither_choice_nor_note_is_invalid", CARD, [door(body(answer={"choice": None, "note": None}), sx1, "body_invalid")], None),
|
||||
sc("a_non_retraction_without_an_answer_is_invalid", CARD, [door(body(answer=None), sx1, "body_invalid")], None),
|
||||
sc("no_session_is_unauthenticated", CARD, [door(x1, srv("n1", None, T(2), None), "unauthenticated")], None),
|
||||
sc("an_unknown_fork_is_refused_at_the_door", CARD, [door(body("no", NOTE, fork="gamecult-chat:question:nope"), sx1, "fork_unknown")], None),
|
||||
sc("V2_stale_hash_is_refused_not_counted_and_stored_as_Refused", CARD, [rf(stale_body(), sx1, "stale_fork", c=CARD)], None),
|
||||
sc("V2_a_stale_answer_then_a_fresh_one_the_fresh_one_is_counted", CARD, [rf(stale_body(), sx1, "stale_fork", c=CARD), ok(x1, sx2, "Counted", None)], "n2"),
|
||||
sc("V3_an_answer_off_the_decider_list_is_Refused_not_a_decider_and_stored", CARD, [rf(z1, sz, "not_a_decider")], None),
|
||||
sc("a_non_decider_with_a_stale_hash_is_stale_first", CARD, [rf(dict(z1, options_hash=STALE_H), sz, "stale_fork", c=CARD)], None),
|
||||
sc("V4_a_closed_fork_refuses_and_stores_the_refusal", FORKC, [rf(x1, sx1, "fork_closed")], None),
|
||||
sc("V5_second_answer_from_one_decider_is_a_Revision_the_first_stays_counted", CARD, [X1, ok(x2, sx2, "Revision", "n1")], "n1"),
|
||||
sc("V5_receiver_order_decides_not_answered_at", CARD, [ok(x2, srv("n1", OP, T(2)), "Counted", None), ok(x1, srv("n2", OP, T(3)), "Revision", "n1")], "n1"),
|
||||
sc("V5_reversed_arrival_reverses_the_result", CARD, [ok(x1, srv("n1", OP, T(2)), "Counted", None), ok(x2, srv("n2", OP, T(3)), "Revision", "n1")], "n1"),
|
||||
sc("a_second_decider_answer_is_recorded_as_a_Revision", Y_CARD, [X1, ok(y1, sy1, "Revision", "n1")], "n1"),
|
||||
sc("a_claim_more_than_five_minutes_ahead_is_accepted_and_flagged", CARD, [ok(body("no", NOTE, at="2026-11-12T19:09:00Z"), sx1, "Counted", None)], "n1"),
|
||||
sc("V6_a_matrix_edit_redaction_or_reaction_changes_nothing", CARD, [X1] + MATRIX, "n1"),
|
||||
sc("V7_a_choice_that_is_not_an_option_is_Refused_choice_invalid", CARD, [rf(body("maybe", None), sx1, "choice_invalid")], None),
|
||||
sc("a_changed_card_stops_the_old_answers_counting_and_a_new_answer_is_counted", CARD,
|
||||
[X1, step("card", None, None, None, card=fork_edit), ok(body("no", NOTE, h=H2), sx2, "Counted", None)], "n2", H2),
|
||||
sc("a_changed_card_leaves_no_counted_answer_until_one_arrives", CARD, [X1, step("card", None, None, None, card=fork_edit)], None, H2),
|
||||
sc("V8_retraction_with_an_answer_is_bad", CARD, [X1, rf(dict(retraction("n1"), answer={"choice": "no", "note": None}), sr("n9"), "bad_retraction")], "n1"),
|
||||
sc("V8_retraction_without_retracts_is_bad", CARD, [X1, rf(dict(retraction("n1"), retracts=None), sr("n9"), "bad_retraction")], "n1"),
|
||||
sc("V8_retracts_on_a_plain_answer_is_bad", CARD, [X1, rf(dict(x2, retracts="n1"), sr("n9"), "bad_retraction")], "n1"),
|
||||
sc("V8_retraction_naming_an_unknown_answer_is_bad", CARD, [X1, rf(retraction("n99"), sr("n9"), "bad_retraction")], "n1"),
|
||||
sc("V8_retraction_naming_a_Refused_answer_is_bad", CARD, [rf(stale_body(), sx1, "stale_fork", c=CARD), rf(retraction("n1"), sr("n9"), "bad_retraction")], None),
|
||||
sc("V8_retracting_twice_is_already_retracted", CARD, [X1, ok(RET("n1"), sr("n8"), "Retraction", "n1"), rf(RET("n1"), sr("n9", m=10), "already_retracted")], None),
|
||||
sc("V8_retraction_under_a_stale_hash_is_stale_fork", CARD, [X1, rf(retraction("n1", h=STALE_H), sr("n9"), "stale_fork", c=CARD)], "n1"),
|
||||
sc("V9_another_decider_retracts_and_the_answer_stops_counting", Y_CARD, [X1, ok(RET("n1"), sr("n7", HM, 9, "Steward"), "Retraction", "n1")], None),
|
||||
sc("V9_an_account_off_the_list_cannot_retract", CARD, [X1, rf(RET("n1"), sr("n6", ST, 9, "Member"), "not_a_decider")], "n1"),
|
||||
sc("V10_after_a_ruling_from_the_answer_retraction_is_ruled", card(ruled_from=["n1"]), [X1, rf(RET("n1"), sr("n9"), "ruled")], "n1"),
|
||||
sc("V11_retracting_the_counted_answer_makes_the_next_decider_answer_counted", Y_CARD, [X1, ok(y1, sy1, "Revision", "n1"), ok(RET("n1"), sr("n8"), "Retraction", "n1")], "n3"),
|
||||
sc("V11_retracting_an_answer_that_was_not_counted_changes_nothing", Y_CARD, [X1, ok(y1, sy1, "Revision", "n1"), ok(RET("n3"), sr("n8"), "Retraction", "n3")], "n1"),
|
||||
sc("after_every_answer_is_retracted_a_fresh_answer_is_Counted_again", CARD, [X1, ok(RET("n1"), sr("n8"), "Retraction", "n1"), ok(x2, sx2, "Counted", None)], "n2"),
|
||||
]
|
||||
for n, c, steps, r in scen:
|
||||
if n.startswith("V10"): assert c["status"] == "open"
|
||||
REASON_CODES = ["stale_fork", "not_a_decider", "fork_closed", "choice_invalid", "bad_retraction", "already_retracted", "ruled"]
|
||||
HTTP = {"unauthenticated": 401, "body_forbidden_field": 400, "body_unknown_field": 400, "body_invalid": 400, "fork_unknown": 404,
|
||||
"stale_fork": 409, "not_a_decider": 403, "fork_closed": 409, "choice_invalid": 400, "bad_retraction": 400, "already_retracted": 409, "ruled": 409}
|
||||
STORED_REFUSALS = []
|
||||
for reason in REASON_CODES:
|
||||
a = da("Refused", None, reason, x1, sx1)
|
||||
STORED_REFUSALS.append({"name": reason, "httpStatus": HTTP[reason], "message": result(a, CARD if reason == "stale_fork" else None)})
|
||||
# ruling citation: authority Operator must cite a Counted answer record whose answerer holds Operator authority
|
||||
CNT = da("Counted", None, None, x1, sx1); REV = da("Revision", "n1", None, x2, sx2)
|
||||
REF = da("Refused", None, "fork_closed", x1, srv("n5", OP, T(7))); NONOP = da("Counted", None, None, z1, sz)
|
||||
def cite(authority, ans, ruling="cultlib-gaps:resolution:question.slot-mark-consent.n1"):
|
||||
return {"schemaVersion": "gamecult.fork.ruling_cite.v0", "ruling_id": ruling, "authority": authority, "answer_id": ans}
|
||||
cites = [
|
||||
{"name": "operator_cites_a_counted_operator_answer", "answers": [CNT], "message": cite("Operator", "n1"), "admit": True},
|
||||
{"name": "operator_cites_nothing", "answers": [CNT], "message": cite("Operator", None), "admit": False},
|
||||
{"name": "operator_cites_a_revision", "answers": [CNT, REV], "message": cite("Operator", "n2"), "admit": False},
|
||||
{"name": "operator_cites_a_refused_answer", "answers": [REF], "message": cite("Operator", "n5"), "admit": False},
|
||||
{"name": "operator_cites_a_counted_answer_by_a_non_operator", "answers": [NONOP], "message": cite("Operator", "n4"), "admit": False},
|
||||
{"name": "operator_cites_an_answer_that_does_not_exist", "answers": [CNT], "message": cite("Operator", "n99"), "admit": False},
|
||||
{"name": "steward_needs_no_citation", "answers": [], "message": cite("Steward", None), "admit": True},
|
||||
]
|
||||
for cz in cites: cz["messagePackHex"] = hx(cz["message"])
|
||||
def bad(name, why, m, **kw): return dict({"name": name, "why": why, "message": m}, **kw)
|
||||
fork = {
|
||||
"provenance": "Rule source: /opt/commons/handoff/fork-desk-answer-binding-20261110.md (Heimdall), amended by the Persona briefs of 2026-11-10 (Eureka's fit check). Bytes spec-authored with msgpack-python (scripts/gen-governance-vectors.py); every runtime re-encodes `message` and must match messagePackHex. options_hash follows fork-card.md 'Options hash'; the vectors named heimdall_* are that file's, with its digests.",
|
||||
"optionsHash": hashes,
|
||||
"card": {"valid": [{"name": "operator_fork", "message": CARD, "messagePackHex": hx(CARD)},
|
||||
{"name": "leans_flag_closed_two_deciders", "message": card(leans="yes", flag="provisional: Self expects this to move", deciders=[OP, HM], depends=[], raised_in=None, status="closed", ruling="x:resolution:y", ruled_from=["n1"], order=0),
|
||||
"messagePackHex": hx(card(leans="yes", flag="provisional: Self expects this to move", deciders=[OP, HM], depends=[], raised_in=None, status="closed", ruling="x:resolution:y", ruled_from=["n1"], order=0))}],
|
||||
"invalid": [bad(n, w, m) for n, w, m in [
|
||||
("one_option", "two or more options", card(options=[OPT[0]], recommendation="yes")),
|
||||
("no_deciders", "a fork with no decider cannot be answered", card(deciders=[])),
|
||||
("null_recommendation", "every card recommends an option", dict(card(), recommendation=None)),
|
||||
("unknown_key", "closed document", dict(card(), note="x")),
|
||||
("option_with_a_label", "options are {id,text}; the old format's display label is not a member (fork-card.md, Old format)", card(options=[dict(OPT[0], label="Yes"), OPT[1]])),
|
||||
("status_unknown", "status is open or closed", card(status="provisional")),
|
||||
("options_hash_not_hex", "options_hash is 64 lowercase hex", dict(card(), options_hash="XYZ")),
|
||||
("duplicate_option_ids", "rule: option ids are unique (not expressible in the schema)", card(options=[OPT[0], dict(OPT[1], id=OPT[0]["id"])])),
|
||||
("recommendation_not_an_option", "rule: recommendation is an option id (not expressible in the schema)", card(recommendation="maybe")),
|
||||
("leans_not_an_option", "rule: leans is nil or an option id (not expressible in the schema)", card(leans="maybe")),
|
||||
("duplicate_deciders", "rule: deciders are unique (not expressible in the schema)", card(deciders=[OP, OP])),
|
||||
("closed_without_ruling", "rule: a closed card names its ruling (not expressible in the schema)", card(status="closed")),
|
||||
("options_hash_of_another_card", "rule: options_hash equals the hash of the shown members (not expressible in the schema)", dict(card(), options_hash=STALE_H)),
|
||||
]]},
|
||||
"request": {"valid": [{"name": n, "message": m, "messagePackHex": hx(m)} for n, m in
|
||||
[("choice_and_note", x1), ("choice_alone", pick), ("note_alone_clear_pick", words), ("retraction", retraction("n1"))]],
|
||||
"invalid": [dict(bad(n, w, m), messagePackHex=hx(m)) for n, w, m in [
|
||||
("answer_with_neither", "an answer object holds a choice, a note, or both", body(answer={"choice": None, "note": None})),
|
||||
("blank_note", "a blank note is no answer", body(None, " \u00a0 ")),
|
||||
("note_over_4000", "notes are at most 4000 characters", body(None, "x" * 4001)),
|
||||
("options_hash_uppercase", "hash is lowercase hex", body("no", None, h=H.upper())),
|
||||
("answerer_key", "answerer is server-set; the door answers body_forbidden_field", dict(x1, answerer=OP)),
|
||||
("authority_key", "authority is server-set; the door answers body_forbidden_field", dict(x1, authority="Operator")),
|
||||
("answer_object_with_a_key", "the answer object is closed", body(answer={"choice": "no", "note": None, "weight": 1})),
|
||||
("answered_at_without_zone", "instants are UTC with a Z", body("no", None, at="2026-11-12T19:02:00")),
|
||||
("retracted_not_a_boolean", "retracted is a boolean", dict(x1, retracted="no")),
|
||||
("fork_id_empty", "fork_id names a fork", body("no", None, fork="")),
|
||||
]]},
|
||||
"deskAnswer": {"valid": [{"name": n, "message": m, "messagePackHex": hx(m)} for n, m in
|
||||
[("counted", CNT), ("revision", REV), ("retraction", da("Retraction", "n1", None, retraction("n1"), sr("n8"))),
|
||||
("refused_stale", da("Refused", None, "stale_fork", stale_body(), sx1)),
|
||||
("refused_ruled_retraction_keeps_no_target", da("Refused", None, "ruled", retraction("n1"), sr("n9")))]],
|
||||
"invalid": [dict(bad(n, w, m), messagePackHex=hx(m)) for n, w, m in [
|
||||
("counted_with_a_code", "only Refused carries a code", dict(CNT, code="ruled")),
|
||||
("refused_without_a_code", "Refused carries a reason code", dict(REF, code=None)),
|
||||
("refused_with_an_unknown_code", "the code set is closed", dict(REF, code="because")),
|
||||
("counted_naming_another_answer", "Counted names none", dict(CNT, of="n0")),
|
||||
("revision_naming_none", "a Revision names the counted answer", dict(REV, of=None)),
|
||||
("revision_that_also_retracts", "a variant carries only its own link (a retraction with no target, or a target with no retraction, cannot exist)", dict(REV, retracts="n1")),
|
||||
("retraction_naming_no_target", "a Retraction names the answer it retracts", dict(da("Retraction", "n1", None, retraction("n1"), sr("n8")), retracts=None)),
|
||||
("refused_naming_a_target", "Refused carries only its code", dict(REF, retracts="n1")),
|
||||
("retraction_carrying_a_choice", "a Retraction carries no choice or note", dict(da("Retraction", "n1", None, retraction("n1"), sr("n8")), choice="no")),
|
||||
("outcome_lower_case", "outcome tags are Counted, Revision, Retraction, Refused", dict(CNT, outcome="counted")),
|
||||
("no_answerer", "answerer is stamped on every stored answer", dict(CNT, answerer="")),
|
||||
]]},
|
||||
"results": [{"name": r["name"], "httpStatus": r["httpStatus"], "message": r["message"], "messagePackHex": hx(r["message"])} for r in STORED_REFUSALS],
|
||||
"doorRefusals": [{"name": c, "httpStatus": HTTP[c], "message": refusal_doc(c), "messagePackHex": hx(refusal_doc(c))} for c in TAIL],
|
||||
"rulingCites": cites,
|
||||
"scenarios": [{"name": n, "card": c, "optionsHash": c["options_hash"], "steps": steps, "record": r, "recordHex": hx(r)} for n, c, steps, r in scen],
|
||||
}
|
||||
(OUT / "fork-card-vectors.json").write_text(json.dumps(fork, indent=2, ensure_ascii=False) + "\n", encoding="utf-8")
|
||||
print("ok", len(vote["valid"]), len(vote["invalid"]), len(fork["scenarios"]))
|
||||
422
tests/GameCult.Networking.Tests/GovernanceVectorTests.cs
Normal file
422
tests/GameCult.Networking.Tests/GovernanceVectorTests.cs
Normal file
|
|
@ -0,0 +1,422 @@
|
|||
#nullable enable
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Nodes;
|
||||
using MessagePack;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace GameCult.Networking.Tests
|
||||
{
|
||||
// contracts/cultnet/governance: the vote-API and fork-card contracts (vote-api.md, fork-card.md). The vectors are spec-authored
|
||||
// (scripts/gen-governance-vectors.py); this suite re-encodes every message with MessagePack C#, must reproduce the bytes, judges
|
||||
// every negative with the committed schema, and replays the fork scenarios through a reference judge written from fork-card.md.
|
||||
public sealed class GovernanceVectorTests
|
||||
{
|
||||
private const string VoteSchema = "governance/vote-api.schema.json";
|
||||
private const string ForkSchema = "governance/fork-card.schema.json";
|
||||
|
||||
private static string RepoRoot()
|
||||
{
|
||||
var directory = new DirectoryInfo(TestContext.CurrentContext.TestDirectory);
|
||||
while (directory != null && !File.Exists(Path.Combine(directory.FullName, "CultLib.sln")))
|
||||
directory = directory.Parent;
|
||||
return directory?.FullName ?? throw new InvalidOperationException("CultLib.sln was not found above the test directory.");
|
||||
}
|
||||
|
||||
private static JsonNode Load(string name) =>
|
||||
JsonNode.Parse(File.ReadAllText(Path.Combine(RepoRoot(), "contracts", "cultnet", "governance", name)))!;
|
||||
|
||||
private static readonly JsonNode Vote = Load("vote-api-vectors.json");
|
||||
private static readonly JsonNode Fork = Load("fork-card-vectors.json");
|
||||
private static MiniJsonSchemaValidator Validator() => new(Path.Combine(RepoRoot(), "contracts", "cultnet"));
|
||||
|
||||
private static List<string> Errors(string file, string def, JsonNode? value)
|
||||
{
|
||||
var element = JsonSerializer.SerializeToElement(value);
|
||||
return Validator().Validate(element, $"{file}#/$defs/{def}");
|
||||
}
|
||||
|
||||
// MessagePack C# writes the message in the order the keys appear: shortest ints, shortest strings.
|
||||
private static void Write(ref MessagePackWriter w, JsonNode? n)
|
||||
{
|
||||
switch (n)
|
||||
{
|
||||
case null: w.WriteNil(); break;
|
||||
case JsonObject o:
|
||||
w.WriteMapHeader(o.Count);
|
||||
foreach (var kv in o) { w.Write(kv.Key); Write(ref w, kv.Value); }
|
||||
break;
|
||||
case JsonArray a:
|
||||
w.WriteArrayHeader(a.Count);
|
||||
foreach (var item in a) Write(ref w, item);
|
||||
break;
|
||||
case JsonValue v when v.TryGetValue<bool>(out var b): w.Write(b); break;
|
||||
case JsonValue v when v.TryGetValue<long>(out var l): w.Write(l); break;
|
||||
case JsonValue v: w.Write(v.GetValue<string>()); break;
|
||||
}
|
||||
}
|
||||
|
||||
private static string HexOf(JsonNode? message)
|
||||
{
|
||||
var buffer = new System.Buffers.ArrayBufferWriter<byte>();
|
||||
var writer = new MessagePackWriter(buffer);
|
||||
Write(ref writer, message);
|
||||
writer.Flush();
|
||||
return Convert.ToHexString(buffer.WrittenSpan);
|
||||
}
|
||||
|
||||
private static string Sha(byte[] bytes) => Convert.ToHexString(SHA256.HashData(bytes)).ToLowerInvariant();
|
||||
private static string S(JsonNode? n) => n!.GetValue<string>();
|
||||
private static JsonNode Clone(JsonNode? n) => JsonNode.Parse(n!.ToJsonString())!;
|
||||
|
||||
[Test]
|
||||
public void VoteApi_ValidDocuments_ValidateEncodeToTheVectorBytesAndDecodeBack()
|
||||
{
|
||||
foreach (var v in Vote["valid"]!.AsArray())
|
||||
{
|
||||
var name = S(v!["name"]);
|
||||
Assert.That(Errors(VoteSchema, S(v["def"]), v["message"]), Is.Empty, name);
|
||||
Assert.That(HexOf(v["message"]), Is.EqualTo(S(v["messagePackHex"])), name);
|
||||
var decoded = JsonNode.Parse(MessagePackSerializer.ConvertToJson(Convert.FromHexString(S(v["messagePackHex"]))))!;
|
||||
Assert.That(JsonNode.DeepEquals(decoded, v["message"]), Is.True, name);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void VoteApi_EveryNegative_IsRefusedBySchemaAndKeepsItsBytes()
|
||||
{
|
||||
foreach (var v in Vote["invalid"]!.AsArray())
|
||||
{
|
||||
var name = S(v!["name"]);
|
||||
Assert.That(Errors(VoteSchema, S(v["def"]), v["message"]), Is.Not.Empty, $"{name}: {S(v["why"])}");
|
||||
Assert.That(HexOf(v["message"]), Is.EqualTo(S(v["messagePackHex"])), name);
|
||||
}
|
||||
}
|
||||
|
||||
private static byte[] CommitmentBytes(JsonNode b, string voter) => Encoding.UTF8.GetBytes(
|
||||
$"gamecult.sealed_ballot.v0\n{S(b["motionId"])}\n{voter}\n{S(b["persona"])}\n{b["round"]!.GetValue<long>()}\n{S(b["candidate"])}\n{S(b["reason"])}\n{S(b["salt"])}");
|
||||
|
||||
[Test]
|
||||
public void VoteApi_Commitment_IsSha256OfTheCanonicalBytes_AndRevealMatchesExactlyWhenItHashesToIt()
|
||||
{
|
||||
foreach (var c in Vote["commitment"]!.AsArray())
|
||||
{
|
||||
var bytes = CommitmentBytes(c!["ballot"]!, S(c["voterUserAccountId"]));
|
||||
Assert.That(Convert.ToHexString(bytes), Is.EqualTo(S(c["canonicalHex"])), S(c["name"]));
|
||||
Assert.That(Sha(bytes), Is.EqualTo(S(c["sha256"])), S(c["name"]));
|
||||
}
|
||||
foreach (var p in Vote["pairs"]!.AsArray())
|
||||
{
|
||||
var digest = Sha(CommitmentBytes(p!["reveal"]!["ballot"]!, S(p["voterUserAccountId"])));
|
||||
Assert.That(digest == S(p["commit"]!["commitment"]), Is.EqualTo(p["matches"]!.GetValue<bool>()), S(p["name"]));
|
||||
Assert.That(Errors(VoteSchema, "cast", p["commit"]), Is.Empty);
|
||||
Assert.That(Errors(VoteSchema, "cast", p["reveal"]), Is.Empty);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void VoteApi_EachRefusalCode_HasAVectorAStatusAndTheVectorBytes()
|
||||
{
|
||||
var schema = JsonNode.Parse(File.ReadAllText(Path.Combine(RepoRoot(), "contracts", "cultnet", VoteSchema)))!;
|
||||
var codes = schema["$defs"]!["refusal"]!["properties"]!["code"]!["enum"]!.AsArray().Select(S).OrderBy(x => x, StringComparer.Ordinal);
|
||||
var vectors = Vote["refusals"]!.AsArray();
|
||||
Assert.That(vectors.Select(r => S(r!["message"]!["code"])).OrderBy(x => x, StringComparer.Ordinal), Is.EqualTo(codes));
|
||||
foreach (var r in vectors)
|
||||
{
|
||||
Assert.That(Errors(VoteSchema, "refusal", r!["message"]), Is.Empty, S(r["name"]));
|
||||
Assert.That(HexOf(r["message"]), Is.EqualTo(S(r["messagePackHex"])), S(r["name"]));
|
||||
Assert.That(r["httpStatus"]!.GetValue<int>(), Is.GreaterThanOrEqualTo(400));
|
||||
}
|
||||
Assert.That(vectors.Single(r => S(r!["name"]) == "rate_limited")!["message"]!["retryAfterSeconds"]!.GetValue<int>(), Is.GreaterThanOrEqualTo(1));
|
||||
}
|
||||
|
||||
// ---- fork card (rule source: /opt/commons/handoff/fork-desk-answer-binding-20261110.md) ----
|
||||
private static string Jstr(string s)
|
||||
{
|
||||
var sb = new StringBuilder("\"");
|
||||
foreach (var rune in s.EnumerateRunes())
|
||||
{
|
||||
var o = rune.Value;
|
||||
sb.Append(o switch
|
||||
{
|
||||
'"' => "\\\"", '\\' => "\\\\", 8 => "\\b", 9 => "\\t", 10 => "\\n", 12 => "\\f", 13 => "\\r",
|
||||
< 0x20 => "\\u" + o.ToString("x4"),
|
||||
_ => rune.ToString()
|
||||
});
|
||||
}
|
||||
return sb.Append('"').ToString();
|
||||
}
|
||||
|
||||
// RFC 8785 over the five shown members, every string NFC-normalised first; keys sorted; no numbers in the member set.
|
||||
private static string N(string s) => s.Normalize(NormalizationForm.FormC);
|
||||
private static byte[] Canonical(JsonNode c)
|
||||
{
|
||||
var options = string.Join(",", c["options"]!.AsArray().Select(o => $"{{\"id\":{Jstr(N(S(o!["id"])))},\"text\":{Jstr(N(S(o["text"])))}}}"));
|
||||
var depends = string.Join(",", c["depends"]!.AsArray().Select(d => Jstr(N(S(d)))));
|
||||
return Encoding.UTF8.GetBytes($"{{\"context\":{Jstr(N(S(c["context"])))},\"depends\":[{depends}],\"options\":[{options}],\"recommendation\":{Jstr(N(S(c["recommendation"])))},\"title\":{Jstr(N(S(c["title"])))}}}");
|
||||
}
|
||||
private static string HashOf(JsonNode c) => Sha(Canonical(c));
|
||||
|
||||
private static IEnumerable<string> CardRuleErrors(JsonNode c)
|
||||
{
|
||||
var ids = c["options"]!.AsArray().Select(o => S(o!["id"])).ToList();
|
||||
if (ids.Distinct().Count() != ids.Count) yield return "duplicate option ids";
|
||||
if (!ids.Contains(S(c["recommendation"]))) yield return "recommendation is not an option";
|
||||
if (c["leans"] is not null && !ids.Contains(S(c["leans"]))) yield return "leans is not an option";
|
||||
var deciders = c["deciders"]!.AsArray().Select(S).ToList();
|
||||
if (deciders.Distinct().Count() != deciders.Count) yield return "duplicate deciders";
|
||||
if (S(c["status"]) == "closed" && c["ruling"] is null) yield return "closed without a ruling";
|
||||
if (S(c["options_hash"]) != HashOf(c)) yield return "options_hash is not the hash of the shown members";
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ForkCard_OptionsHash_IsSha256OfTheCanonicalJson_ComputedHere_AndEveryShownMemberMovesIt()
|
||||
{
|
||||
foreach (var h in Fork["optionsHash"]!.AsArray())
|
||||
{
|
||||
var bytes = Canonical(h!);
|
||||
Assert.That(Encoding.UTF8.GetString(bytes), Is.EqualTo(S(h["canonical"])), S(h["name"]));
|
||||
Assert.That(Convert.ToHexString(bytes), Is.EqualTo(S(h["canonicalHex"])), S(h["name"]));
|
||||
Assert.That(Sha(bytes), Is.EqualTo(S(h["sha256"])), S(h["name"]));
|
||||
}
|
||||
var byName = Fork["optionsHash"]!.AsArray().ToDictionary(h => S(h!["name"]), h => S(h!["sha256"]));
|
||||
var baseline = byName["heimdall_A_five_members"];
|
||||
Assert.That(byName["hidden_members_do_not_move_it"], Is.EqualTo(baseline));
|
||||
Assert.That(byName["heimdall_E_decomposed_equals_D"], Is.EqualTo(byName["heimdall_D_nfc_escapes_astral"]), "an NFD and an NFC spelling hash alike");
|
||||
foreach (var moved in new[] { "heimdall_B_recommendation_changed", "heimdall_C_option_order_changed", "heimdall_F_option_id_changed", "heimdall_G_depends_emptied",
|
||||
"option_text_changed", "title_changed", "context_changed", "depends_order_changed", "depends_order_changed_reversed" })
|
||||
Assert.That(byName[moved], Is.Not.EqualTo(baseline), moved);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ForkCard_MutatingAnyShownMember_AnOptionIdIncluded_ChangesTheHash()
|
||||
{
|
||||
var card = Fork["card"]!["valid"]![0]!["message"]!;
|
||||
var baseline = HashOf(card);
|
||||
var mutations = new Action<JsonNode>[]
|
||||
{
|
||||
c => c["title"] = S(c["title"]) + "x", c => c["context"] = S(c["context"]) + "x",
|
||||
c => c["options"]![0]!["id"] = S(c["options"]![0]!["id"]) + "x", c => c["options"]![0]!["text"] = S(c["options"]![0]!["text"]) + "x",
|
||||
c => { var o = c["options"]!.AsArray(); var first = o[0]!; o.RemoveAt(0); o.Add(first); },
|
||||
c => c["recommendation"] = S(c["options"]![0]!["id"]) == S(c["recommendation"]) ? S(c["options"]![1]!["id"]) : S(c["options"]![0]!["id"]),
|
||||
c => c["depends"]!.AsArray().Add("a:b:c"), c => c["depends"]!.AsArray().Clear(),
|
||||
};
|
||||
for (var i = 0; i < mutations.Length; i++)
|
||||
{
|
||||
var copy = Clone(card);
|
||||
mutations[i](copy);
|
||||
Assert.That(HashOf(copy), Is.Not.EqualTo(baseline), $"mutation {i}");
|
||||
}
|
||||
var hidden = Clone(card);
|
||||
hidden["fork_id"] = "other:question:id"; hidden["leans"] = "yes"; hidden["status"] = "closed"; hidden["ruled_from"] = new JsonArray("n1");
|
||||
Assert.That(HashOf(hidden), Is.EqualTo(baseline), "fork_id and the unshown members stay outside the hash");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ForkCard_DocumentsValidateAndEncodeToTheVectorBytes()
|
||||
{
|
||||
foreach (var v in Fork["card"]!["valid"]!.AsArray())
|
||||
{
|
||||
Assert.That(Errors(ForkSchema, "card", v!["message"]), Is.Empty, S(v["name"]));
|
||||
Assert.That(CardRuleErrors(v["message"]!), Is.Empty, S(v["name"]));
|
||||
Assert.That(HexOf(v["message"]), Is.EqualTo(S(v["messagePackHex"])), S(v["name"]));
|
||||
}
|
||||
foreach (var (section, def) in new[] { ("request", "answerRequest"), ("deskAnswer", "deskAnswer") })
|
||||
foreach (var v in Fork[section]!["valid"]!.AsArray())
|
||||
{
|
||||
Assert.That(Errors(ForkSchema, def, v!["message"]), Is.Empty, S(v["name"]));
|
||||
Assert.That(HexOf(v["message"]), Is.EqualTo(S(v["messagePackHex"])), S(v["name"]));
|
||||
var decoded = JsonNode.Parse(MessagePackSerializer.ConvertToJson(Convert.FromHexString(S(v["messagePackHex"]))))!;
|
||||
Assert.That(JsonNode.DeepEquals(decoded, v["message"]), Is.True, S(v["name"]));
|
||||
}
|
||||
var schema = JsonNode.Parse(File.ReadAllText(Path.Combine(RepoRoot(), "contracts", "cultnet", ForkSchema)))!;
|
||||
IEnumerable<string> Codes(string def) => schema["$defs"]![def]!["properties"]!["code"]!["enum"]!.AsArray().Select(S).OrderBy(x => x, StringComparer.Ordinal);
|
||||
Assert.That(Fork["results"]!.AsArray().Select(r => S(r!["name"])).OrderBy(x => x, StringComparer.Ordinal), Is.EqualTo(Codes("deskAnswerRefused")));
|
||||
foreach (var v in Fork["results"]!.AsArray())
|
||||
{
|
||||
Assert.That(Errors(ForkSchema, "answerResult", v!["message"]), Is.Empty, S(v["name"]));
|
||||
Assert.That(HexOf(v["message"]), Is.EqualTo(S(v["messagePackHex"])), S(v["name"]));
|
||||
Assert.That(v["message"]!["card"] is not null, Is.EqualTo(S(v["name"]) == "stale_fork"), "only a stale refusal shows the new fork");
|
||||
}
|
||||
Assert.That(Fork["doorRefusals"]!.AsArray().Select(r => S(r!["name"])).OrderBy(x => x, StringComparer.Ordinal), Is.EqualTo(Codes("refusal")));
|
||||
foreach (var v in Fork["doorRefusals"]!.AsArray())
|
||||
{
|
||||
Assert.That(Errors(ForkSchema, "refusal", v!["message"]), Is.Empty, S(v["name"]));
|
||||
Assert.That(HexOf(v["message"]), Is.EqualTo(S(v["messagePackHex"])), S(v["name"]));
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ForkCard_EveryNegative_IsRefusedBySchemaOrAStatedRule()
|
||||
{
|
||||
foreach (var v in Fork["card"]!["invalid"]!.AsArray())
|
||||
Assert.That(Errors(ForkSchema, "card", v!["message"]).Count > 0 || CardRuleErrors(v["message"]!).Any(), Is.True, $"{S(v["name"])}: {S(v["why"])}");
|
||||
foreach (var (section, def) in new[] { ("request", "answerRequest"), ("deskAnswer", "deskAnswer") })
|
||||
foreach (var v in Fork[section]!["invalid"]!.AsArray())
|
||||
{
|
||||
Assert.That(Errors(ForkSchema, def, v!["message"]), Is.Not.Empty, $"{S(v["name"])}: {S(v["why"])}");
|
||||
Assert.That(HexOf(v["message"]), Is.EqualTo(S(v["messagePackHex"])), S(v["name"]));
|
||||
}
|
||||
}
|
||||
|
||||
private static readonly Dictionary<string, string> Tails = new()
|
||||
{
|
||||
["unauthenticated"] = "a verified session is required",
|
||||
["body_forbidden_field"] = "answerer and authority are set by the receiver",
|
||||
["body_unknown_field"] = "the body is a closed shape",
|
||||
["body_invalid"] = "the body does not satisfy gamecult.fork.answer_request.v0",
|
||||
["fork_unknown"] = "no such fork",
|
||||
};
|
||||
private static readonly string[] RequestKeys = { "schemaVersion", "fork_id", "options_hash", "answer", "retracted", "retracts", "answered_at" };
|
||||
|
||||
private static bool HasKey(JsonNode? n, params string[] names) => n switch
|
||||
{
|
||||
JsonObject o => o.Any(kv => names.Contains(kv.Key) || HasKey(kv.Value, names)),
|
||||
JsonArray a => a.Any(x => HasKey(x, names)),
|
||||
_ => false
|
||||
};
|
||||
|
||||
// The reference judge: Huginn's half behind the second door as fork-card.md states it. Server-set fields come from the step.
|
||||
private static (List<JsonNode?> Responses, JsonNode Record) Judge(JsonNode startCard, JsonArray steps)
|
||||
{
|
||||
var card = startCard;
|
||||
var answers = new JsonArray();
|
||||
var responses = new List<JsonNode?>();
|
||||
string? Counted()
|
||||
{
|
||||
var hash = HashOf(card);
|
||||
foreach (var a in answers)
|
||||
{
|
||||
if (S(a!["outcome"]) is not ("Counted" or "Revision") || S(a["options_hash"]) != hash) continue;
|
||||
if (answers.Any(r => S(r!["outcome"]) == "Retraction" && S(r["retracts"]) == S(a["answer_id"]))) continue;
|
||||
return S(a["answer_id"]);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
foreach (var step in steps)
|
||||
{
|
||||
var kind = S(step!["kind"]);
|
||||
if (kind == "matrix_event") { responses.Add(null); continue; } // rule 9: Matrix never changes a decision
|
||||
if (kind == "card") { card = step["card"]!; responses.Add(null); continue; }
|
||||
var s = step["server"]!;
|
||||
var b = step["body"]!.AsObject();
|
||||
JsonNode Door(string code) => new JsonObject { ["schemaVersion"] = "gamecult.fork.refusal.v0", ["code"] = code, ["error"] = $"{code}: {Tails[code]}" };
|
||||
JsonNode Store(string outcome, string? link, string? code)
|
||||
{
|
||||
var a = b["answer"] as JsonObject;
|
||||
var doc = new JsonObject
|
||||
{
|
||||
["schemaVersion"] = "gamecult.desk_answer.v0", ["answer_id"] = S(s["answer_id"]), ["fork_id"] = Clone(b["fork_id"]), ["outcome"] = outcome,
|
||||
["of"] = outcome == "Revision" ? link : null, ["retracts"] = outcome == "Retraction" ? link : null, ["code"] = outcome == "Refused" ? code : null,
|
||||
["options_hash"] = Clone(b["options_hash"]), ["choice"] = Clone(a?["choice"]), ["note"] = Clone(a?["note"]), ["answered_at"] = Clone(b["answered_at"]),
|
||||
["received_at"] = Clone(s["received_at"]),
|
||||
["claim_in_future"] = (DateTimeOffset.Parse(S(b["answered_at"])) - DateTimeOffset.Parse(S(s["received_at"]))).TotalSeconds > 300,
|
||||
["answerer"] = Clone(s["answerer"]), ["authority"] = Clone(s["authority"])
|
||||
};
|
||||
answers.Add(Clone(doc));
|
||||
return new JsonObject { ["schemaVersion"] = "gamecult.fork.answer_result.v0", ["answer"] = doc, ["card"] = code == "stale_fork" ? Clone(card) : null };
|
||||
}
|
||||
JsonNode Refuse(string code) => Store("Refused", null, code);
|
||||
var deciders = card["deciders"]!.AsArray().Select(S).ToList();
|
||||
var retracted = b["retracted"] is JsonValue rv && rv.TryGetValue<bool>(out var rb) && rb;
|
||||
JsonNode response;
|
||||
if (s["answerer"] is null) response = Door("unauthenticated");
|
||||
else if (HasKey(b, "answerer", "authority")) response = Door("body_forbidden_field");
|
||||
else if (b.Any(kv => !RequestKeys.Contains(kv.Key)) || (b["answer"] is JsonObject ao && ao.Any(kv => kv.Key is not ("choice" or "note")))) response = Door("body_unknown_field");
|
||||
else if (Errors(ForkSchema, "answerRequest", b).Count > 0 || (!retracted && b["answer"] is null)) response = Door("body_invalid");
|
||||
else if (S(b["fork_id"]) != S(card["fork_id"])) response = Door("fork_unknown");
|
||||
else if (S(b["options_hash"]) != HashOf(card)) response = Refuse("stale_fork");
|
||||
else if (!deciders.Contains(S(s["answerer"]))) response = Refuse("not_a_decider");
|
||||
else if (S(card["status"]) == "closed") response = Refuse("fork_closed");
|
||||
else if (retracted || b["retracts"] is not null)
|
||||
{
|
||||
var target = answers.FirstOrDefault(a => a is not null && b["retracts"] is not null && S(a["answer_id"]) == S(b["retracts"])
|
||||
&& S(a["outcome"]) is "Counted" or "Revision" && S(a["options_hash"]) == S(b["options_hash"]));
|
||||
if (!retracted || b["answer"] is not null || b["retracts"] is null || target is null) response = Refuse("bad_retraction");
|
||||
else if (answers.Any(a => S(a!["outcome"]) == "Retraction" && S(a["retracts"]) == S(target["answer_id"]))) response = Refuse("already_retracted");
|
||||
else if (card["ruled_from"]!.AsArray().Any(r => S(r) == S(target["answer_id"]))) response = Refuse("ruled");
|
||||
else response = Store("Retraction", S(target["answer_id"]), null);
|
||||
}
|
||||
else if (b["answer"]!["choice"] is not null && !card["options"]!.AsArray().Any(o => S(o!["id"]) == S(b["answer"]!["choice"]))) response = Refuse("choice_invalid");
|
||||
else
|
||||
{
|
||||
var first = Counted();
|
||||
response = first is null ? Store("Counted", null, null) : Store("Revision", first, null);
|
||||
}
|
||||
responses.Add(response);
|
||||
}
|
||||
var record = new JsonObject
|
||||
{
|
||||
["schemaVersion"] = "gamecult.fork.record.v0", ["fork_id"] = Clone(card["fork_id"]), ["options_hash"] = HashOf(card), ["counted_answer_id"] = Counted(), ["answers"] = answers
|
||||
};
|
||||
return (responses, record);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ForkCard_EveryScenario_ReplaysToTheVectorsResponsesAndRecord_BytesIncluded()
|
||||
{
|
||||
var seen = new HashSet<string>();
|
||||
var doorRefusals = 0;
|
||||
var door = new HashSet<string> { "unauthenticated", "body_forbidden_field", "body_unknown_field", "body_invalid", "fork_unknown" };
|
||||
foreach (var sc in Fork["scenarios"]!.AsArray())
|
||||
{
|
||||
var name = S(sc!["name"]);
|
||||
Assert.That(HashOf(sc["card"]!), Is.EqualTo(S(sc["optionsHash"])), name);
|
||||
Assert.That(CardRuleErrors(sc["card"]!), Is.Empty, name);
|
||||
var steps = sc["steps"]!.AsArray();
|
||||
var (responses, record) = Judge(sc["card"]!, steps);
|
||||
Assert.That(responses.Count, Is.EqualTo(steps.Count), name);
|
||||
for (var i = 0; i < steps.Count; i++)
|
||||
Assert.That(JsonNode.DeepEquals(responses[i], steps[i]!["response"]), Is.True, $"{name} step {i}: {responses[i]?.ToJsonString()}");
|
||||
Assert.That(JsonNode.DeepEquals(record, sc["record"]), Is.True, $"{name}: {record.ToJsonString()}");
|
||||
Assert.That(Errors(ForkSchema, "record", record), Is.Empty, name);
|
||||
Assert.That(HexOf(record), Is.EqualTo(S(sc["recordHex"])), name);
|
||||
foreach (var step in steps)
|
||||
{
|
||||
var r = step!["response"];
|
||||
if (r is null) continue;
|
||||
Assert.That(Errors(ForkSchema, "answerResult", r).Count == 0 || Errors(ForkSchema, "refusal", r).Count == 0, Is.True, name);
|
||||
if (r["code"] is not null)
|
||||
{
|
||||
seen.Add(S(r["code"]));
|
||||
if (door.Contains(S(r["code"])))
|
||||
{
|
||||
doorRefusals++;
|
||||
Assert.That(sc["record"]!["answers"]!.AsArray().Any(a => S(a!["answer_id"]) == S(step["server"]!["answer_id"])), Is.False, $"{name} stored a door refusal");
|
||||
}
|
||||
}
|
||||
else seen.Add(S(r["answer"]!["code"] ?? r["answer"]!["outcome"]));
|
||||
}
|
||||
foreach (var a in sc["record"]!["answers"]!.AsArray()) seen.Add(S(a!["outcome"]));
|
||||
}
|
||||
Assert.That(doorRefusals, Is.GreaterThanOrEqualTo(8));
|
||||
foreach (var want in new[] { "Counted", "Revision", "Retraction", "Refused", "unauthenticated", "body_forbidden_field", "body_unknown_field", "body_invalid", "fork_unknown", "stale_fork",
|
||||
"not_a_decider", "fork_closed", "choice_invalid", "bad_retraction", "already_retracted", "ruled" })
|
||||
Assert.That(seen, Does.Contain(want));
|
||||
}
|
||||
|
||||
// A ruling may carry authority Operator only if it cites a Counted desk_answer stamped Operator (binding file 1.3d).
|
||||
private static bool AdmitRuling(JsonNode cite, JsonArray answers)
|
||||
{
|
||||
if (S(cite["authority"]) != "Operator") return true;
|
||||
var cited = answers.FirstOrDefault(a => cite["answer_id"] is not null && S(a!["answer_id"]) == S(cite["answer_id"]));
|
||||
return cited is not null && S(cited["outcome"]) == "Counted" && S(cited["authority"]) == "Operator";
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ForkCard_OperatorAuthority_NeedsACitedCountedAnswerRecord()
|
||||
{
|
||||
foreach (var c in Fork["rulingCites"]!.AsArray())
|
||||
{
|
||||
Assert.That(Errors(ForkSchema, "rulingCite", c!["message"]), Is.Empty, S(c["name"]));
|
||||
Assert.That(HexOf(c["message"]), Is.EqualTo(S(c["messagePackHex"])), S(c["name"]));
|
||||
Assert.That(AdmitRuling(c["message"]!, c["answers"]!.AsArray()), Is.EqualTo(c["admit"]!.GetValue<bool>()), S(c["name"]));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue