forked from yvv/decision
Lot S, phase 2 : le quorum dans le serveur (ADR 0013), vu rouge puis vert
- migration 0011 (additive ; 0010 reste au lot V) : documents.quorums (par cran,
NULL = vote_floor à chaque cran, le C d'avant), units.quorum (NULL = celui du
cran), journaux quorum_changes et quorum_amendments ;
- quorum effectif = units.quorum ?? quorums[cran] ; les propositions d'inertie et
les modules en héritent ; p0 = 2 × vote_floor / W, jamais le quorum d'un article ;
- route PUT /units/{id}/quorum { "quorum": n | null } : trustin_access::sets_quorum
(l'auteur de la première version, un administrateur pour le seed), refusée dès
qu'un vote existe (quorum::voted_units : vote du serveur, bulletin de la chaîne
en attente ou confirmé, adoption) ; bornes 2..W, 1..W dans le groupe d'essai ;
- amendement gardé 2026-11-03-quorums-par-cran : écrit les quorums du seed là où
il n'y en a pas, une fois par document, consigné, file de publication là où il
publie ;
- JSON : evaluation.quorum / yesNeeded / noNeeded ; l'unité : quorum,
quorumDefault, canSetQuorum ; doc.quorums ; levels[].quorum ; document.json
publié : quorums ; adoptions.params : quorum (plus de constant) ;
- seed : quorums 12 · 12 · 50 · 50 · 50 (Monnaie libre), 1 partout (essai).
- tests : tests/quorum.rs (5) et les règles pures ; les scénarios du groupe
d'essai qui demandaient deux voix posent le quorum 2 de l'article, comme son
auteur le ferait ; les chiffres de Monnaie libre suivent (9 au cran 2, 39 au
cran 3, 43 au cran 4).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -15,7 +15,7 @@ use axum::extract::{Path, Query, State};
|
||||
use ld_engine::ballot::Choice;
|
||||
use ld_engine::compose::{self, PreviewMode, ProposalKind, ProposalState, UnitState, VersionText};
|
||||
use ld_engine::inertia::{InertiaLevel, Participation, Scale};
|
||||
use ld_engine::threshold::{Evaluation, Tally};
|
||||
use ld_engine::threshold::{self, Evaluation, FormulaParams, Tally};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::json;
|
||||
use std::collections::{BTreeMap, HashMap, HashSet};
|
||||
@@ -196,8 +196,21 @@ pub struct DocRow {
|
||||
pub title: String,
|
||||
pub summary: String,
|
||||
pub source_lang: String,
|
||||
/// The reference quorum Q₀ (C before 2026-11-03): read for the reference
|
||||
/// participation p0 = 2Q₀/W only, and as every level's quorum while `quorums` is NULL.
|
||||
pub vote_floor: f64,
|
||||
pub publish_path: Option<String>,
|
||||
/// The quorum of each level, JSON `[Q1, …, Q5]` (ADR 0013); NULL before the quorum
|
||||
/// amendment (`quorum_amend`).
|
||||
pub quorums: Option<String>,
|
||||
}
|
||||
|
||||
impl DocRow {
|
||||
/// The quorum of each level, 1 to 5: the document's, or, in a base the quorum
|
||||
/// amendment has not reached yet, its reference quorum at every level — the C of before.
|
||||
pub fn quorums(&self) -> [u32; 5] {
|
||||
self.quorums.as_deref().and_then(|raw| serde_json::from_str(raw).ok()).unwrap_or([whole(self.vote_floor); 5])
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, sqlx::FromRow)]
|
||||
@@ -208,6 +221,16 @@ pub struct UnitRow {
|
||||
pub position: f64,
|
||||
pub inertia: String,
|
||||
pub in_force_id: Option<String>,
|
||||
/// Its own quorum, set by the author of its first version before the first vote
|
||||
/// (`quorum`); NULL: the quorum of its level.
|
||||
pub quorum: Option<i64>,
|
||||
}
|
||||
|
||||
impl UnitRow {
|
||||
/// Its own quorum, if its author set one.
|
||||
pub fn own_quorum(&self) -> Option<u32> {
|
||||
self.quorum.and_then(|q| u32::try_from(q).ok())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, sqlx::FromRow)]
|
||||
@@ -294,7 +317,8 @@ pub struct DocState {
|
||||
|
||||
pub async fn load_doc(state: &AppState, doc_id: &str) -> AppResult<DocRow> {
|
||||
sqlx::query_as::<_, DocRow>(
|
||||
"SELECT id, group_slug, slug, title, summary, source_lang, vote_floor, publish_path FROM documents WHERE id = ?",
|
||||
"SELECT id, group_slug, slug, title, summary, source_lang, vote_floor, publish_path, quorums
|
||||
FROM documents WHERE id = ?",
|
||||
)
|
||||
.bind(doc_id)
|
||||
.fetch_optional(&state.db)
|
||||
@@ -321,7 +345,7 @@ pub async fn load_state(state: &AppState, doc_id: &str) -> AppResult<DocState> {
|
||||
let doc = load_doc(state, doc_id).await?;
|
||||
let group = load_group(state, &doc.group_slug).await?;
|
||||
let units = sqlx::query_as::<_, UnitRow>(
|
||||
"SELECT id, code, section, position, inertia, in_force_id FROM units WHERE doc_id = ? ORDER BY position, id",
|
||||
"SELECT id, code, section, position, inertia, in_force_id, quorum FROM units WHERE doc_id = ? ORDER BY position, id",
|
||||
)
|
||||
.bind(doc_id)
|
||||
.fetch_all(&state.db)
|
||||
@@ -397,9 +421,25 @@ pub async fn load_state(state: &AppState, doc_id: &str) -> AppResult<DocState> {
|
||||
}
|
||||
|
||||
impl DocState {
|
||||
/// What this document's votes are judged by: W, C and p̃.
|
||||
/// What this document's votes are judged by: W, p̃ and the quorum of each level.
|
||||
pub fn scale(&self) -> Scale {
|
||||
Scale { electorate: self.electorate, floor: self.doc.vote_floor, participation: self.participation.ratio }
|
||||
Scale { electorate: self.electorate, participation: self.participation.ratio, quorums: self.doc.quorums() }
|
||||
}
|
||||
|
||||
/// The quorum an article is judged on, and its versions with it: its own, else its
|
||||
/// level's (`Scale::quorum`).
|
||||
pub fn quorum_of(&self, unit: &UnitRow) -> u32 {
|
||||
self.scale().quorum(inertia_of(unit), unit.own_quorum())
|
||||
}
|
||||
|
||||
/// The quorum of the article's level: what it is judged on without one of its own.
|
||||
pub fn quorum_default(&self, unit: &UnitRow) -> u32 {
|
||||
self.scale().quorum(inertia_of(unit), None)
|
||||
}
|
||||
|
||||
/// The letters a proposal is judged by: its level and its article's quorum.
|
||||
pub fn params(&self, unit: &UnitRow, p: &ProposalRow) -> FormulaParams {
|
||||
compose::params_for(&bare(unit), p.kind(), &self.scale())
|
||||
}
|
||||
|
||||
/// p̃ · W, as a whole number of voters.
|
||||
@@ -422,7 +462,7 @@ impl DocState {
|
||||
|
||||
/// How far the threshold is, as shown: on `shown_tally`.
|
||||
pub fn shown_evaluation(&self, unit: &UnitRow, p: &ProposalRow) -> Option<Evaluation> {
|
||||
self.scale().evaluate(self.shown_tally(&p.id), self.voting_level(unit, p)).ok()
|
||||
threshold::evaluate(self.shown_tally(&p.id), self.electorate, &self.params(unit, p)).ok()
|
||||
}
|
||||
|
||||
/// Open to a vote: open or in force, and not held back by an objection.
|
||||
@@ -511,6 +551,7 @@ impl DocState {
|
||||
id: u.id.clone(),
|
||||
position: u.position,
|
||||
inertia: inertia_of(u),
|
||||
quorum: u.own_quorum(),
|
||||
in_force: u.in_force_id.as_deref().and_then(|id| by_id.get(id)).map(|p| match live.get(&u.id) {
|
||||
Some(order) => VersionText {
|
||||
id: p.id.clone(),
|
||||
@@ -563,13 +604,12 @@ impl DocState {
|
||||
}
|
||||
|
||||
pub fn evaluation(&self, unit: &UnitRow, p: &ProposalRow) -> Option<Evaluation> {
|
||||
self.scale().evaluate(self.tally(&p.id), self.voting_level(unit, p)).ok()
|
||||
threshold::evaluate(self.tally(&p.id), self.electorate, &self.params(unit, p)).ok()
|
||||
}
|
||||
|
||||
/// The level a proposal is voted at — a change of inertia inherits its article's.
|
||||
pub fn voting_level(&self, unit: &UnitRow, p: &ProposalRow) -> InertiaLevel {
|
||||
let us = UnitState { id: unit.id.clone(), position: unit.position, inertia: inertia_of(unit), in_force: None };
|
||||
compose::voting_level(&us, p.kind())
|
||||
compose::voting_level(&bare(unit), p.kind())
|
||||
}
|
||||
|
||||
/// The usual participation, as the screen tells it.
|
||||
@@ -583,22 +623,36 @@ impl DocState {
|
||||
})
|
||||
}
|
||||
|
||||
/// The five levels on this scale: M, G, s_k, and the *for* votes asked at `cast` votes.
|
||||
pub fn levels_json(&self, cast: u32) -> Vec<serde_json::Value> {
|
||||
/// The five levels on this scale: M, G, s_k, the quorum, and the *for* votes asked at
|
||||
/// `cast` votes — on an article's own quorum if it has one (`own`), else each level's.
|
||||
pub fn levels_json(&self, cast: u32, own: Option<u32>) -> Vec<serde_json::Value> {
|
||||
let scale = self.scale();
|
||||
InertiaLevel::ALL
|
||||
.iter()
|
||||
.map(|k| {
|
||||
let params = scale.params(*k);
|
||||
let quorum = scale.quorum(*k, own);
|
||||
let params = scale.params(*k, quorum);
|
||||
json!({
|
||||
"level": k, "majorityPct": params.majority_pct, "gradient": params.gradient,
|
||||
"sharePct": k.share_pct(), "cast": cast, "required": scale.required(cast, *k).ok(),
|
||||
"sharePct": k.share_pct(), "quorum": quorum, "cast": cast,
|
||||
"required": scale.required(cast, *k, quorum).ok(),
|
||||
})
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
/// A unit as the engine sees it, without its text: its level and its own quorum.
|
||||
fn bare(unit: &UnitRow) -> UnitState {
|
||||
UnitState {
|
||||
id: unit.id.clone(),
|
||||
position: unit.position,
|
||||
inertia: inertia_of(unit),
|
||||
quorum: unit.own_quorum(),
|
||||
in_force: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// A non-negative count, rounded to the nearest whole.
|
||||
fn whole(x: f64) -> u32 {
|
||||
if x.is_finite() && x > 0.0 { x.round().min(f64::from(u32::MAX)) as u32 } else { 0 }
|
||||
@@ -705,6 +759,7 @@ pub async fn document(
|
||||
let pending = ballots::my_ballots(&state, &user.id, &id).await?;
|
||||
|
||||
let changes = inertia_changes(&state, &id).await?;
|
||||
let rights = crate::quorum::Rights::of(&state, &id).await?;
|
||||
let unit_json = |u: &UnitRow| {
|
||||
let of_unit: Vec<_> = visible
|
||||
.iter()
|
||||
@@ -723,11 +778,18 @@ pub async fn document(
|
||||
.as_ref()
|
||||
.and_then(|fid| of_unit.iter().find(|p| p["id"].as_str() == Some(fid.as_str())).cloned());
|
||||
let proposals: Vec<_> = of_unit.into_iter().filter(|p| p["status"] != "in_force").collect();
|
||||
json!({
|
||||
let mut j = json!({
|
||||
"id": u.id, "code": u.code, "section": u.section, "position": u.position, "inertia": inertia_of(u),
|
||||
"inertiaChanges": changes.get(&u.id).cloned().unwrap_or_default(),
|
||||
"inForce": in_force, "proposals": proposals,
|
||||
})
|
||||
});
|
||||
// Its quorum (ADR 0013): its own or `null`, its level's, and who may set it.
|
||||
if let (Some(o), serde_json::Value::Object(q)) =
|
||||
(j.as_object_mut(), crate::quorum::unit_json(&st, &rights, &user, u))
|
||||
{
|
||||
o.extend(q);
|
||||
}
|
||||
j
|
||||
};
|
||||
let units: Vec<_> = st.articles().map(unit_json).collect();
|
||||
// The modules (ADR 0009): the title of the document, the order of each section.
|
||||
@@ -757,7 +819,7 @@ pub async fn document(
|
||||
"doc": {
|
||||
"id": st.doc.id, "slug": st.doc.slug, "groupSlug": st.group.slug, "groupName": st.group.name,
|
||||
"title": util::parse_lang(&st.doc.title), "summary": util::parse_lang(&st.doc.summary),
|
||||
"sourceLang": st.doc.source_lang, "voteFloor": st.doc.vote_floor,
|
||||
"sourceLang": st.doc.source_lang, "voteFloor": st.doc.vote_floor, "quorums": st.doc.quorums(),
|
||||
"electorate": st.electorate, "electorateSource": st.electorate_source,
|
||||
"formula": { "base": ld_engine::FormulaParams::DEFAULT_BASE },
|
||||
"participation": st.participation_json(),
|
||||
@@ -766,7 +828,7 @@ pub async fn document(
|
||||
"units": units,
|
||||
"modules": { "title": title, "orders": orders },
|
||||
// The five levels, with the *for* votes asked at the usual participation.
|
||||
"levels": st.levels_json(st.usual_voters()),
|
||||
"levels": st.levels_json(st.usual_voters(), None),
|
||||
})))
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user