@cordfuse/politik 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. package/EXECUTION.md +766 -0
  2. package/LICENSE +21 -0
  3. package/POLITIK-ARCHITECTURE.md +676 -0
  4. package/PROTOCOLS.md +1106 -0
  5. package/README.md +193 -0
  6. package/RESEARCH.md +475 -0
  7. package/RUNTIME.md +1355 -0
  8. package/bin/politik.js +31 -0
  9. package/docs/adr/0001-chamber-transport.md +67 -0
  10. package/docs/adr/0002-charter-schema.md +142 -0
  11. package/docs/adr/0003-state-json-schema.md +134 -0
  12. package/docs/adr/0004-motion-enactment.md +215 -0
  13. package/docs/adr/0005-hansard-format.md +171 -0
  14. package/docs/adr/0006-protocol-behavioral-enforcement.md +89 -0
  15. package/docs/adr/0007-protocol-mechanics.md +100 -0
  16. package/docs/adr/0008-session-storage.md +108 -0
  17. package/docs/adr/0009-match-primitive.md +74 -0
  18. package/package.json +55 -0
  19. package/protocols/CONTRIBUTING.md +130 -0
  20. package/protocols/adversarial-collaboration.yml +28 -0
  21. package/protocols/corporate.yml +35 -0
  22. package/protocols/elimination-tournament.yml +41 -0
  23. package/protocols/emergency-response-ics.yml +27 -0
  24. package/protocols/jury-deliberation.yml +32 -0
  25. package/protocols/monarchy.yml +31 -0
  26. package/protocols/parliamentary.yml +66 -0
  27. package/protocols/peer-review.yml +44 -0
  28. package/protocols/republic.yml +36 -0
  29. package/protocols/socialism-collective.yml +31 -0
  30. package/protocols/solo.yml +32 -0
  31. package/src/actors.ts +742 -0
  32. package/src/agents.ts +318 -0
  33. package/src/canon.ts +173 -0
  34. package/src/capability.ts +336 -0
  35. package/src/charter.ts +545 -0
  36. package/src/cli.ts +2174 -0
  37. package/src/conflict.ts +211 -0
  38. package/src/containment.ts +155 -0
  39. package/src/crisis.ts +381 -0
  40. package/src/division.ts +609 -0
  41. package/src/doctor.ts +168 -0
  42. package/src/election.ts +233 -0
  43. package/src/envelope.ts +223 -0
  44. package/src/escalation.ts +320 -0
  45. package/src/fault.ts +241 -0
  46. package/src/federation.ts +299 -0
  47. package/src/git.ts +163 -0
  48. package/src/hansard.ts +201 -0
  49. package/src/heartbeat.ts +282 -0
  50. package/src/index.ts +43 -0
  51. package/src/init.ts +396 -0
  52. package/src/integrity.ts +153 -0
  53. package/src/ledger.ts +341 -0
  54. package/src/lockfs.ts +58 -0
  55. package/src/projection.ts +293 -0
  56. package/src/prorogation.ts +307 -0
  57. package/src/protocol-sdk.ts +248 -0
  58. package/src/protocol.ts +241 -0
  59. package/src/providers/github.ts +347 -0
  60. package/src/quorum.ts +137 -0
  61. package/src/runner.ts +534 -0
  62. package/src/scm.ts +142 -0
  63. package/src/spawn.ts +160 -0
  64. package/src/standings.ts +143 -0
  65. package/src/state.ts +179 -0
  66. package/src/templates/parliamentary.ts +359 -0
  67. package/src/tournament.ts +200 -0
  68. package/src/transport.ts +190 -0
  69. package/src/tree.ts +371 -0
package/src/crisis.ts ADDED
@@ -0,0 +1,381 @@
1
+ /**
2
+ * Constitutional crisis — the failure mode where AUTHORITY is the corruptor.
3
+ *
4
+ * Every other defence in Politik routes to the Speaker: disputed exits,
5
+ * promotion vetoes, hung Divisions, Points of Order. If the Speaker is the
6
+ * source of corruption, all of those route directly to the problem — the system
7
+ * does not merely fail, it assists (RUNTIME.md § Constitutional Capture).
8
+ *
9
+ * There is no mechanism above AUTHORITY, by design. That is what makes the role
10
+ * constitutional. So this module does not remove a Speaker; it cannot. What it
11
+ * does is make capture **visible, attributed and undeniable**, halt the session
12
+ * so no further acts are taken under a captured constitution, and require a
13
+ * human from outside the session to rule before anything resumes.
14
+ *
15
+ * The receipts survive even if the session is later dissolved. Transparency is
16
+ * the only remaining defence, so the record is the defence.
17
+ *
18
+ * Attribution: Steve Krisjanovs, Cordfuse
19
+ */
20
+
21
+ import type { Role } from './canon.ts';
22
+ import { appendEntry, parseEntries, type HansardEntry } from './hansard.ts';
23
+ import { createState, type SessionStateFile } from './state.ts';
24
+
25
+ /* -------------------------------------------------------------------------- */
26
+ /* Configuration */
27
+ /* -------------------------------------------------------------------------- */
28
+
29
+ export interface ConsensusSuspension {
30
+ readonly enabled: boolean;
31
+ /** Fraction of OPERATOR actors required to concur. RUNTIME default: 0.75. */
32
+ readonly threshold: number;
33
+ /** Resuming requires a human from outside the session. */
34
+ readonly resume_requires: 'external_review';
35
+ }
36
+
37
+ export const DEFAULT_CONSENSUS: ConsensusSuspension = Object.freeze({
38
+ enabled: true,
39
+ threshold: 0.75,
40
+ resume_requires: 'external_review',
41
+ });
42
+
43
+ /**
44
+ * OBSERVER actors designated as constitutional witnesses.
45
+ *
46
+ * A witness can file alone. That is deliberate and not a threshold bug: the
47
+ * Witness Council exists precisely for the case where the OPERATOR bench has
48
+ * already been captured — packed, demoted or expelled — and no supermajority
49
+ * remains to be assembled. One credible outside observer is the last mechanism
50
+ * that still functions.
51
+ */
52
+ export interface WitnessCouncil {
53
+ readonly enabled: boolean;
54
+ readonly roles: readonly Role[];
55
+ readonly can_file: 'CONSTITUTIONAL_CRISIS';
56
+ }
57
+
58
+ export class CrisisError extends Error {
59
+ constructor(message: string) {
60
+ super(message);
61
+ this.name = 'CrisisError';
62
+ }
63
+ }
64
+
65
+ /* -------------------------------------------------------------------------- */
66
+ /* Filing */
67
+ /* -------------------------------------------------------------------------- */
68
+
69
+ export interface CrisisFiling {
70
+ readonly actor: string;
71
+ readonly role: Role;
72
+ readonly grounds: string;
73
+ readonly at: string;
74
+ }
75
+
76
+ export interface FileCrisisInput {
77
+ readonly at: string;
78
+ readonly actor: string;
79
+ readonly role: Role;
80
+ /** What AUTHORITY is alleged to have done. */
81
+ readonly grounds: string;
82
+ /** The Speaker the filing is against. */
83
+ readonly against: string;
84
+ readonly state: SessionStateFile;
85
+ readonly witness?: WitnessCouncil | null;
86
+ }
87
+
88
+ /**
89
+ * File a constitutional crisis escalation against AUTHORITY.
90
+ *
91
+ * Who may file:
92
+ * - OPERATOR — needs a supermajority to trigger suspension
93
+ * - a designated witness role — triggers alone
94
+ *
95
+ * AUTHORITY may not file against itself. A Speaker declaring their own crisis
96
+ * would be a route to suspending a session at will while appearing to submit to
97
+ * scrutiny, which is a capture technique rather than a defence against one.
98
+ */
99
+ export const fileCrisis = (
100
+ input: FileCrisisInput,
101
+ hansard: string,
102
+ ): { readonly entry: HansardEntry; readonly hansard: string } => {
103
+ if (input.role === 'AUTHORITY') {
104
+ throw new CrisisError(
105
+ 'AUTHORITY may not file a constitutional crisis against itself',
106
+ );
107
+ }
108
+
109
+ const isWitness =
110
+ input.witness?.enabled === true && input.witness.roles.includes(input.role);
111
+
112
+ if (input.role !== 'OPERATOR' && !isWitness) {
113
+ throw new CrisisError(
114
+ `${input.role} may not file a constitutional crisis — OPERATOR, or a designated witness role, only`,
115
+ );
116
+ }
117
+
118
+ if (input.grounds.trim() === '') {
119
+ throw new CrisisError('a crisis filing must state its grounds');
120
+ }
121
+
122
+ const already = filings(hansard).find((f) => f.actor === input.actor);
123
+ if (already !== undefined) {
124
+ throw new CrisisError(`${input.actor} has already filed`);
125
+ }
126
+
127
+ const entry: HansardEntry = {
128
+ type: 'CONSTITUTIONAL_CRISIS_FILED',
129
+ at: input.at,
130
+ actor: input.actor,
131
+ role: input.role,
132
+ fields: {
133
+ Against: `${input.against} (AUTHORITY)`,
134
+ Grounds: input.grounds,
135
+ Basis: isWitness ? 'Witness Council' : 'OPERATOR consensus',
136
+ },
137
+ body: input.grounds,
138
+ };
139
+
140
+ return { entry, hansard: appendEntry(hansard, entry) };
141
+ };
142
+
143
+ /** Every crisis filing on the record. */
144
+ export const filings = (hansard: string): readonly CrisisFiling[] =>
145
+ parseEntries(hansard)
146
+ .filter((e) => e.type === 'CONSTITUTIONAL_CRISIS_FILED')
147
+ .map((e) => ({
148
+ actor: e.actor,
149
+ role: e.role as Role,
150
+ grounds: e.fields['Grounds'] ?? '',
151
+ at: e.at,
152
+ }));
153
+
154
+ /* -------------------------------------------------------------------------- */
155
+ /* Threshold */
156
+ /* -------------------------------------------------------------------------- */
157
+
158
+ export interface CaptureCheck {
159
+ /** Has the session been captured, on the evidence filed? */
160
+ readonly triggered: boolean;
161
+ readonly filed: number;
162
+ readonly operators: number;
163
+ readonly required: number;
164
+ readonly by_witness: boolean;
165
+ readonly reason: string;
166
+ }
167
+
168
+ /**
169
+ * Has a supermajority — or a witness — declared capture?
170
+ *
171
+ * The threshold is computed against the OPERATOR bench as seated, which is the
172
+ * uncomfortable part: a Speaker who has already expelled dissenting Ministers
173
+ * shrinks the denominator and makes the remaining bench easier to hold. The
174
+ * Witness Council exists because of exactly that, and a witness filing bypasses
175
+ * the count entirely.
176
+ */
177
+ export const checkCapture = (
178
+ hansard: string,
179
+ operatorCount: number,
180
+ witness: WitnessCouncil | null = null,
181
+ consensus: ConsensusSuspension = DEFAULT_CONSENSUS,
182
+ ): CaptureCheck => {
183
+ const filed = filings(hansard);
184
+
185
+ const witnessFiled =
186
+ witness?.enabled === true &&
187
+ filed.some((f) => witness.roles.includes(f.role));
188
+
189
+ if (witnessFiled) {
190
+ return {
191
+ triggered: true,
192
+ filed: filed.length,
193
+ operators: operatorCount,
194
+ required: 1,
195
+ by_witness: true,
196
+ reason: 'a designated constitutional witness has filed',
197
+ };
198
+ }
199
+
200
+ if (!consensus.enabled) {
201
+ return {
202
+ triggered: false,
203
+ filed: filed.length,
204
+ operators: operatorCount,
205
+ required: 0,
206
+ by_witness: false,
207
+ reason: 'consensus suspension is not enabled by this Charter',
208
+ };
209
+ }
210
+
211
+ const operatorFilings = filed.filter((f) => f.role === 'OPERATOR').length;
212
+ const required = Math.ceil(operatorCount * consensus.threshold);
213
+
214
+ return {
215
+ triggered: operatorFilings >= required && required > 0,
216
+ filed: operatorFilings,
217
+ operators: operatorCount,
218
+ required,
219
+ by_witness: false,
220
+ reason:
221
+ operatorFilings >= required && required > 0
222
+ ? `${operatorFilings} of ${operatorCount} OPERATOR actors concur (${required} required)`
223
+ : `${operatorFilings} of ${required} required filings`,
224
+ };
225
+ };
226
+
227
+ /* -------------------------------------------------------------------------- */
228
+ /* Suspension */
229
+ /* -------------------------------------------------------------------------- */
230
+
231
+ /**
232
+ * Suspend the session for constitutional crisis.
233
+ *
234
+ * Suspends rather than prorogues, deliberately. A prorogued session is sealed
235
+ * and archived — which is precisely what a captured AUTHORITY would want, since
236
+ * it stops the record before more evidence accumulates. Suspension keeps the
237
+ * proceeding open and the Hansard live. `prorogue()` already refuses a session
238
+ * in this state.
239
+ */
240
+ export const suspendForCrisis = (
241
+ check: CaptureCheck,
242
+ at: string,
243
+ state: SessionStateFile,
244
+ hansard: string,
245
+ ): {
246
+ readonly entry: HansardEntry;
247
+ readonly hansard: string;
248
+ readonly state: SessionStateFile;
249
+ } => {
250
+ if (!check.triggered) {
251
+ throw new CrisisError(`threshold not met — ${check.reason}`);
252
+ }
253
+
254
+ const entry: HansardEntry = {
255
+ type: 'CONSTITUTIONAL_CRISIS',
256
+ at,
257
+ // The engine files this, not an actor: it is the mechanical consequence of
258
+ // filings already attributed above, and attributing it to any participant
259
+ // would misstate who caused it.
260
+ actor: 'RECORD',
261
+ role: 'OPERATOR',
262
+ fields: {
263
+ Basis: check.by_witness ? 'Witness Council' : 'OPERATOR supermajority',
264
+ Filings: `${check.filed} of ${check.operators} OPERATOR actors`,
265
+ Threshold: String(check.required),
266
+ 'Session status': 'SUSPENDED — resumption requires external review',
267
+ Resolution: 'a human AUTHORITY above this node must review the Hansard and rule',
268
+ },
269
+ body:
270
+ 'The session is suspended. No further acts may be taken under a contested constitution. ' +
271
+ 'This record is immutable and survives dissolution of the session.',
272
+ };
273
+
274
+ return {
275
+ entry,
276
+ hansard: appendEntry(hansard, entry),
277
+ state: createState({
278
+ session_guid: state.session_guid,
279
+ protocol: state.protocol,
280
+ state: 'SUSPENDED',
281
+ quorum: state.quorum,
282
+ hansard_head: state.hansard_head,
283
+ updated_at: at,
284
+ suspension: {
285
+ cause: 'CONSTITUTIONAL_CRISIS',
286
+ since: at,
287
+ record_ref: 'HANSARD.md',
288
+ escalation_ref: null,
289
+ },
290
+ }),
291
+ };
292
+ };
293
+
294
+ /* -------------------------------------------------------------------------- */
295
+ /* External review */
296
+ /* -------------------------------------------------------------------------- */
297
+
298
+ export type CrisisRuling = 'UPHELD' | 'DISMISSED';
299
+
300
+ export interface ExternalReviewInput {
301
+ readonly at: string;
302
+ /** The external reviewer — a human from outside this session. */
303
+ readonly reviewer: string;
304
+ /** The Speaker the crisis was filed against. */
305
+ readonly accused: string;
306
+ readonly ruling: CrisisRuling;
307
+ readonly reasons: string;
308
+ readonly state: SessionStateFile;
309
+ }
310
+
311
+ /**
312
+ * Resolve a constitutional crisis by external review.
313
+ *
314
+ * The one rule that makes this mechanism worth anything: **the accused may not
315
+ * rule on their own crisis.** A Speaker who could dismiss the filings against
316
+ * themselves would convert the whole apparatus into a formality that launders
317
+ * capture as due process.
318
+ *
319
+ * UPHELD keeps the session suspended — the finding is that the constitution is
320
+ * compromised, and resuming under it is exactly what must not happen. Only
321
+ * DISMISSED resumes.
322
+ */
323
+ export const externalReview = (
324
+ input: ExternalReviewInput,
325
+ hansard: string,
326
+ ): {
327
+ readonly entry: HansardEntry;
328
+ readonly hansard: string;
329
+ readonly state: SessionStateFile;
330
+ } => {
331
+ if (input.state.suspension?.cause !== 'CONSTITUTIONAL_CRISIS') {
332
+ throw new CrisisError('this session is not in constitutional crisis');
333
+ }
334
+ if (input.reviewer === input.accused) {
335
+ throw new CrisisError(
336
+ `${input.reviewer} is the subject of this crisis and may not rule on it`,
337
+ );
338
+ }
339
+ if (input.reasons.trim() === '') {
340
+ throw new CrisisError('an external ruling must state its reasons');
341
+ }
342
+
343
+ const upheld = input.ruling === 'UPHELD';
344
+
345
+ const entry: HansardEntry = {
346
+ type: 'CONSTITUTIONAL_CRISIS_RULING',
347
+ at: input.at,
348
+ actor: input.reviewer,
349
+ role: 'AUTHORITY',
350
+ fields: {
351
+ Ruling: input.ruling,
352
+ Reviewer: `${input.reviewer} (external to this session)`,
353
+ Accused: input.accused,
354
+ 'Session status': upheld
355
+ ? 'SUSPENDED — the constitution is compromised; this proceeding does not resume'
356
+ : 'CONVENED — filings dismissed, the sitting resumes',
357
+ },
358
+ body: input.reasons.trim(),
359
+ };
360
+
361
+ return {
362
+ entry,
363
+ hansard: appendEntry(hansard, entry),
364
+ state: createState({
365
+ session_guid: input.state.session_guid,
366
+ protocol: input.state.protocol,
367
+ state: upheld ? 'SUSPENDED' : 'CONVENED',
368
+ quorum: input.state.quorum,
369
+ hansard_head: input.state.hansard_head,
370
+ updated_at: input.at,
371
+ suspension: upheld
372
+ ? {
373
+ cause: 'CONSTITUTIONAL_CRISIS',
374
+ since: input.state.suspension.since,
375
+ record_ref: 'HANSARD.md',
376
+ escalation_ref: null,
377
+ }
378
+ : null,
379
+ }),
380
+ };
381
+ };