Spaces:
Sleeping
Sleeping
thewildofficial commited on
Commit ·
9104e4c
1
Parent(s): 3d60dc7
Tighten parliament action validation
Browse filesTrack explicit proposal abstentions and reject votes from unknown ministers while expanding edge-case coverage for action validation and majority resolution.
Made-with: Cursor
- core/__init__.py +2 -0
- core/parliament.py +45 -2
- tests/unit/test_parliament.py +96 -1
core/__init__.py
CHANGED
|
@@ -20,6 +20,7 @@ from .parliament import (
|
|
| 20 |
DebateMessage,
|
| 21 |
ParliamentRoundState,
|
| 22 |
Proposal,
|
|
|
|
| 23 |
ResolutionResult,
|
| 24 |
VoteRecord,
|
| 25 |
)
|
|
@@ -35,6 +36,7 @@ __all__ = [
|
|
| 35 |
"StepResult",
|
| 36 |
"ParliamentRoundState",
|
| 37 |
"Proposal",
|
|
|
|
| 38 |
"VoteRecord",
|
| 39 |
"DebateMessage",
|
| 40 |
"ResolutionResult",
|
|
|
|
| 20 |
DebateMessage,
|
| 21 |
ParliamentRoundState,
|
| 22 |
Proposal,
|
| 23 |
+
ProposalAbstention,
|
| 24 |
ResolutionResult,
|
| 25 |
VoteRecord,
|
| 26 |
)
|
|
|
|
| 36 |
"StepResult",
|
| 37 |
"ParliamentRoundState",
|
| 38 |
"Proposal",
|
| 39 |
+
"ProposalAbstention",
|
| 40 |
"VoteRecord",
|
| 41 |
"DebateMessage",
|
| 42 |
"ResolutionResult",
|
core/parliament.py
CHANGED
|
@@ -53,6 +53,15 @@ class DebateMessage:
|
|
| 53 |
submitted_order: int = 0
|
| 54 |
|
| 55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
@dataclass(frozen=True, slots=True)
|
| 57 |
class ResolutionResult:
|
| 58 |
"""Resolved parliamentary round output for the core game engine."""
|
|
@@ -66,6 +75,7 @@ class ResolutionResult:
|
|
| 66 |
proposals: list[Proposal]
|
| 67 |
votes: list[VoteRecord]
|
| 68 |
debate_messages: list[DebateMessage]
|
|
|
|
| 69 |
|
| 70 |
|
| 71 |
@dataclass(slots=True)
|
|
@@ -78,13 +88,20 @@ class ParliamentRoundState:
|
|
| 78 |
proposals: list[Proposal] = field(default_factory=list)
|
| 79 |
votes: list[VoteRecord] = field(default_factory=list)
|
| 80 |
debate_messages: list[DebateMessage] = field(default_factory=list)
|
|
|
|
| 81 |
_proposal_counter: int = 0
|
| 82 |
_vote_counter: int = 0
|
| 83 |
_debate_counter: int = 0
|
|
|
|
| 84 |
_proposing_agents: set[str] = field(default_factory=set)
|
|
|
|
| 85 |
_votes_by_proposal_agent: set[tuple[str, str]] = field(default_factory=set)
|
| 86 |
|
| 87 |
-
def collect_action(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
"""Collect one structured action if it belongs to the supplied phase."""
|
| 89 |
|
| 90 |
data = _action_to_mapping(action)
|
|
@@ -102,6 +119,10 @@ class ParliamentRoundState:
|
|
| 102 |
amount=data.get("amount"),
|
| 103 |
justification=str(data.get("justification", "")),
|
| 104 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
if Phase(phase) == Phase.VOTING and action_type == ActionType.VOTE.value:
|
| 106 |
return self.submit_vote(
|
| 107 |
agent_id=str(data.get("agent_id") or data.get("agent") or ""),
|
|
@@ -122,6 +143,25 @@ class ParliamentRoundState:
|
|
| 122 |
self.debate_messages.append(debate_message)
|
| 123 |
return debate_message
|
| 124 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
def submit_proposal(
|
| 126 |
self,
|
| 127 |
agent_id: str,
|
|
@@ -131,7 +171,7 @@ class ParliamentRoundState:
|
|
| 131 |
) -> Proposal:
|
| 132 |
"""Validate and record a proposal for the agent's assigned department."""
|
| 133 |
|
| 134 |
-
if agent_id in self._proposing_agents:
|
| 135 |
raise ValueError("duplicate_proposal")
|
| 136 |
if self.agent_departments.get(agent_id) != department:
|
| 137 |
raise ValueError("wrong_department")
|
|
@@ -159,6 +199,8 @@ class ParliamentRoundState:
|
|
| 159 |
proposal = self._proposal_by_id(proposal_id)
|
| 160 |
if proposal is None:
|
| 161 |
raise ValueError("proposal_not_found")
|
|
|
|
|
|
|
| 162 |
if agent_id == proposal.agent_id:
|
| 163 |
raise ValueError("self_vote")
|
| 164 |
|
|
@@ -216,6 +258,7 @@ class ParliamentRoundState:
|
|
| 216 |
proposals=list(self.proposals),
|
| 217 |
votes=list(self.votes),
|
| 218 |
debate_messages=list(self.debate_messages),
|
|
|
|
| 219 |
)
|
| 220 |
|
| 221 |
def _proposal_by_id(self, proposal_id: str) -> Proposal | None:
|
|
|
|
| 53 |
submitted_order: int = 0
|
| 54 |
|
| 55 |
|
| 56 |
+
@dataclass(frozen=True, slots=True)
|
| 57 |
+
class ProposalAbstention:
|
| 58 |
+
"""Explicit record that a minister skipped their proposal turn."""
|
| 59 |
+
|
| 60 |
+
agent_id: str
|
| 61 |
+
department: str
|
| 62 |
+
submitted_order: int = 0
|
| 63 |
+
|
| 64 |
+
|
| 65 |
@dataclass(frozen=True, slots=True)
|
| 66 |
class ResolutionResult:
|
| 67 |
"""Resolved parliamentary round output for the core game engine."""
|
|
|
|
| 75 |
proposals: list[Proposal]
|
| 76 |
votes: list[VoteRecord]
|
| 77 |
debate_messages: list[DebateMessage]
|
| 78 |
+
proposal_abstentions: list[ProposalAbstention]
|
| 79 |
|
| 80 |
|
| 81 |
@dataclass(slots=True)
|
|
|
|
| 88 |
proposals: list[Proposal] = field(default_factory=list)
|
| 89 |
votes: list[VoteRecord] = field(default_factory=list)
|
| 90 |
debate_messages: list[DebateMessage] = field(default_factory=list)
|
| 91 |
+
proposal_abstentions: list[ProposalAbstention] = field(default_factory=list)
|
| 92 |
_proposal_counter: int = 0
|
| 93 |
_vote_counter: int = 0
|
| 94 |
_debate_counter: int = 0
|
| 95 |
+
_abstention_counter: int = 0
|
| 96 |
_proposing_agents: set[str] = field(default_factory=set)
|
| 97 |
+
_abstaining_agents: set[str] = field(default_factory=set)
|
| 98 |
_votes_by_proposal_agent: set[tuple[str, str]] = field(default_factory=set)
|
| 99 |
|
| 100 |
+
def collect_action(
|
| 101 |
+
self,
|
| 102 |
+
phase: Phase,
|
| 103 |
+
action: Any,
|
| 104 |
+
) -> Proposal | VoteRecord | DebateMessage | ProposalAbstention | None:
|
| 105 |
"""Collect one structured action if it belongs to the supplied phase."""
|
| 106 |
|
| 107 |
data = _action_to_mapping(action)
|
|
|
|
| 119 |
amount=data.get("amount"),
|
| 120 |
justification=str(data.get("justification", "")),
|
| 121 |
)
|
| 122 |
+
if Phase(phase) == Phase.PROPOSAL and action_type == ActionType.ABSTAIN_FROM_PROPOSAL.value:
|
| 123 |
+
return self.submit_abstention(
|
| 124 |
+
agent_id=str(data.get("agent_id") or data.get("agent") or ""),
|
| 125 |
+
)
|
| 126 |
if Phase(phase) == Phase.VOTING and action_type == ActionType.VOTE.value:
|
| 127 |
return self.submit_vote(
|
| 128 |
agent_id=str(data.get("agent_id") or data.get("agent") or ""),
|
|
|
|
| 143 |
self.debate_messages.append(debate_message)
|
| 144 |
return debate_message
|
| 145 |
|
| 146 |
+
def submit_abstention(self, agent_id: str) -> ProposalAbstention:
|
| 147 |
+
"""Record that an agent explicitly skipped their proposal turn."""
|
| 148 |
+
|
| 149 |
+
department = self.agent_departments.get(agent_id)
|
| 150 |
+
if department is None:
|
| 151 |
+
raise ValueError("unknown_agent")
|
| 152 |
+
if agent_id in self._proposing_agents or agent_id in self._abstaining_agents:
|
| 153 |
+
raise ValueError("duplicate_abstention")
|
| 154 |
+
|
| 155 |
+
self._abstention_counter += 1
|
| 156 |
+
abstention = ProposalAbstention(
|
| 157 |
+
agent_id=agent_id,
|
| 158 |
+
department=department,
|
| 159 |
+
submitted_order=self._abstention_counter,
|
| 160 |
+
)
|
| 161 |
+
self.proposal_abstentions.append(abstention)
|
| 162 |
+
self._abstaining_agents.add(agent_id)
|
| 163 |
+
return abstention
|
| 164 |
+
|
| 165 |
def submit_proposal(
|
| 166 |
self,
|
| 167 |
agent_id: str,
|
|
|
|
| 171 |
) -> Proposal:
|
| 172 |
"""Validate and record a proposal for the agent's assigned department."""
|
| 173 |
|
| 174 |
+
if agent_id in self._proposing_agents or agent_id in self._abstaining_agents:
|
| 175 |
raise ValueError("duplicate_proposal")
|
| 176 |
if self.agent_departments.get(agent_id) != department:
|
| 177 |
raise ValueError("wrong_department")
|
|
|
|
| 199 |
proposal = self._proposal_by_id(proposal_id)
|
| 200 |
if proposal is None:
|
| 201 |
raise ValueError("proposal_not_found")
|
| 202 |
+
if agent_id not in self.agent_departments:
|
| 203 |
+
raise ValueError("unknown_agent")
|
| 204 |
if agent_id == proposal.agent_id:
|
| 205 |
raise ValueError("self_vote")
|
| 206 |
|
|
|
|
| 258 |
proposals=list(self.proposals),
|
| 259 |
votes=list(self.votes),
|
| 260 |
debate_messages=list(self.debate_messages),
|
| 261 |
+
proposal_abstentions=list(self.proposal_abstentions),
|
| 262 |
)
|
| 263 |
|
| 264 |
def _proposal_by_id(self, proposal_id: str) -> Proposal | None:
|
tests/unit/test_parliament.py
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
|
|
|
|
|
| 1 |
import pytest
|
| 2 |
|
| 3 |
-
from core.parliament import ParliamentRoundState, Proposal, VoteRecord
|
| 4 |
from schemas.actions import ActionType, VoteChoice
|
| 5 |
from schemas.phases import Phase
|
| 6 |
|
|
@@ -60,6 +62,19 @@ def test_negative_amount_rejected():
|
|
| 60 |
)
|
| 61 |
|
| 62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
def test_amount_over_treasury_rejected():
|
| 64 |
state = make_state(treasury=25.0)
|
| 65 |
|
|
@@ -129,6 +144,40 @@ def test_duplicate_vote_rejected():
|
|
| 129 |
)
|
| 130 |
|
| 131 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
def test_yes_majority_approves():
|
| 133 |
state = make_state()
|
| 134 |
proposal = state.submit_proposal(
|
|
@@ -164,6 +213,23 @@ def test_tie_rejects():
|
|
| 164 |
assert result.proposal_outcomes[proposal.proposal_id] == "rejected_tie"
|
| 165 |
|
| 166 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
def test_all_abstain_rejects():
|
| 168 |
state = make_state()
|
| 169 |
proposal = state.submit_proposal(
|
|
@@ -181,6 +247,21 @@ def test_all_abstain_rejects():
|
|
| 181 |
assert result.proposal_outcomes[proposal.proposal_id] == "rejected_all_abstain"
|
| 182 |
|
| 183 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
def test_abstentions_excluded_from_majority():
|
| 185 |
state = ParliamentRoundState(
|
| 186 |
round_number=1,
|
|
@@ -283,3 +364,17 @@ def test_collect_actions_over_phases_and_resolve_to_allocations():
|
|
| 283 |
assert state.debate_messages[0].message == "Health can wait."
|
| 284 |
assert isinstance(state.proposals[0], Proposal)
|
| 285 |
assert isinstance(state.votes[0], VoteRecord)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import math
|
| 2 |
+
|
| 3 |
import pytest
|
| 4 |
|
| 5 |
+
from core.parliament import ParliamentRoundState, Proposal, ProposalAbstention, VoteRecord
|
| 6 |
from schemas.actions import ActionType, VoteChoice
|
| 7 |
from schemas.phases import Phase
|
| 8 |
|
|
|
|
| 62 |
)
|
| 63 |
|
| 64 |
|
| 65 |
+
@pytest.mark.parametrize("amount", [math.nan, math.inf, -math.inf, True])
|
| 66 |
+
def test_non_finite_and_bool_amounts_rejected(amount):
|
| 67 |
+
state = make_state()
|
| 68 |
+
|
| 69 |
+
with pytest.raises(ValueError, match="invalid_amount"):
|
| 70 |
+
state.submit_proposal(
|
| 71 |
+
agent_id="social-minister",
|
| 72 |
+
department="Social",
|
| 73 |
+
amount=amount,
|
| 74 |
+
justification="Invalid amount",
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
|
| 78 |
def test_amount_over_treasury_rejected():
|
| 79 |
state = make_state(treasury=25.0)
|
| 80 |
|
|
|
|
| 144 |
)
|
| 145 |
|
| 146 |
|
| 147 |
+
def test_unknown_voter_rejected():
|
| 148 |
+
state = make_state()
|
| 149 |
+
proposal = state.submit_proposal(
|
| 150 |
+
agent_id="social-minister",
|
| 151 |
+
department="Social",
|
| 152 |
+
amount=10.0,
|
| 153 |
+
justification="Vote target",
|
| 154 |
+
)
|
| 155 |
+
|
| 156 |
+
with pytest.raises(ValueError, match="unknown_agent"):
|
| 157 |
+
state.submit_vote(
|
| 158 |
+
agent_id="unknown-minister",
|
| 159 |
+
proposal_id=proposal.proposal_id,
|
| 160 |
+
vote=VoteChoice.YES,
|
| 161 |
+
)
|
| 162 |
+
|
| 163 |
+
|
| 164 |
+
def test_invalid_vote_value_rejected():
|
| 165 |
+
state = make_state()
|
| 166 |
+
proposal = state.submit_proposal(
|
| 167 |
+
agent_id="social-minister",
|
| 168 |
+
department="Social",
|
| 169 |
+
amount=10.0,
|
| 170 |
+
justification="Vote target",
|
| 171 |
+
)
|
| 172 |
+
|
| 173 |
+
with pytest.raises(ValueError, match="invalid_vote"):
|
| 174 |
+
state.submit_vote(
|
| 175 |
+
agent_id="health-minister",
|
| 176 |
+
proposal_id=proposal.proposal_id,
|
| 177 |
+
vote="MAYBE",
|
| 178 |
+
)
|
| 179 |
+
|
| 180 |
+
|
| 181 |
def test_yes_majority_approves():
|
| 182 |
state = make_state()
|
| 183 |
proposal = state.submit_proposal(
|
|
|
|
| 213 |
assert result.proposal_outcomes[proposal.proposal_id] == "rejected_tie"
|
| 214 |
|
| 215 |
|
| 216 |
+
def test_exact_fifty_percent_yes_boundary_rejects():
|
| 217 |
+
state = make_state()
|
| 218 |
+
proposal = state.submit_proposal(
|
| 219 |
+
agent_id="social-minister",
|
| 220 |
+
department="Social",
|
| 221 |
+
amount=10.0,
|
| 222 |
+
justification="Boundary target",
|
| 223 |
+
)
|
| 224 |
+
state.submit_vote("agriculture-minister", proposal.proposal_id, VoteChoice.YES)
|
| 225 |
+
state.submit_vote("health-minister", proposal.proposal_id, VoteChoice.NO)
|
| 226 |
+
|
| 227 |
+
result = state.resolve()
|
| 228 |
+
|
| 229 |
+
assert result.allocations["Social"] == 0.0
|
| 230 |
+
assert result.proposal_outcomes[proposal.proposal_id] == "rejected_tie"
|
| 231 |
+
|
| 232 |
+
|
| 233 |
def test_all_abstain_rejects():
|
| 234 |
state = make_state()
|
| 235 |
proposal = state.submit_proposal(
|
|
|
|
| 247 |
assert result.proposal_outcomes[proposal.proposal_id] == "rejected_all_abstain"
|
| 248 |
|
| 249 |
|
| 250 |
+
def test_zero_vote_proposal_rejects_as_all_abstain():
|
| 251 |
+
state = make_state()
|
| 252 |
+
proposal = state.submit_proposal(
|
| 253 |
+
agent_id="social-minister",
|
| 254 |
+
department="Social",
|
| 255 |
+
amount=10.0,
|
| 256 |
+
justification="No vote target",
|
| 257 |
+
)
|
| 258 |
+
|
| 259 |
+
result = state.resolve()
|
| 260 |
+
|
| 261 |
+
assert result.allocations["Social"] == 0.0
|
| 262 |
+
assert result.proposal_outcomes[proposal.proposal_id] == "rejected_all_abstain"
|
| 263 |
+
|
| 264 |
+
|
| 265 |
def test_abstentions_excluded_from_majority():
|
| 266 |
state = ParliamentRoundState(
|
| 267 |
round_number=1,
|
|
|
|
| 364 |
assert state.debate_messages[0].message == "Health can wait."
|
| 365 |
assert isinstance(state.proposals[0], Proposal)
|
| 366 |
assert isinstance(state.votes[0], VoteRecord)
|
| 367 |
+
|
| 368 |
+
|
| 369 |
+
def test_collect_action_routes_abstain_from_proposal():
|
| 370 |
+
state = make_state()
|
| 371 |
+
|
| 372 |
+
abstention = state.collect_action(
|
| 373 |
+
Phase.PROPOSAL,
|
| 374 |
+
{"type": ActionType.ABSTAIN_FROM_PROPOSAL, "agent_id": "health-minister"},
|
| 375 |
+
)
|
| 376 |
+
|
| 377 |
+
assert isinstance(abstention, ProposalAbstention)
|
| 378 |
+
assert abstention.agent_id == "health-minister"
|
| 379 |
+
assert abstention.department == "Health"
|
| 380 |
+
assert state.proposal_abstentions == [abstention]
|