@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
@@ -0,0 +1,336 @@
1
+ /**
2
+ * Actor capability profiles — the five dimensions.
3
+ *
4
+ * A Charter declares what a constituency *requires*; an actor declares what
5
+ * they *are*. This module decides whether the two match, at Writ Drop and again
6
+ * before an agent is spawned.
7
+ *
8
+ * The dimensions (POLITIK-ARCHITECTURE.md § Actor Capability Profile):
9
+ *
10
+ * 1. Role tier — trust and verbs. Already enforced elsewhere.
11
+ * 2. Personality archetype — how the actor collaborates under pressure.
12
+ * 3. Soft skills — how it reasons and communicates.
13
+ * 4. Hard skills — domain competence, `required` and `excluded`.
14
+ * 5. Model / knowledge grade — the underlying intelligence, per seat.
15
+ *
16
+ * `excluded` matters as much as `required`, and is the less obvious of the two:
17
+ * an actor working outside its domain does not fail loudly, it produces
18
+ * confident wrong output. Excluding a skill is how a Charter says "not this,
19
+ * even though you could".
20
+ *
21
+ * This module is also the instrument the research layer depends on. If profile
22
+ * composition has measurable effects, the profiles have to be real and recorded
23
+ * rather than declared and ignored.
24
+ *
25
+ * Attribution: Steve Krisjanovs, Cordfuse
26
+ */
27
+
28
+ import { parse as parseYaml } from 'yaml';
29
+
30
+ import { splitFrontmatter, type Constituency } from './charter.ts';
31
+ import type { HansardEntry } from './hansard.ts';
32
+ import type { Role } from './canon.ts';
33
+
34
+ /* -------------------------------------------------------------------------- */
35
+ /* Dimensions 2 and 3 */
36
+ /* -------------------------------------------------------------------------- */
37
+
38
+ export const ARCHETYPES = [
39
+ 'HARDLINER',
40
+ 'DIPLOMAT',
41
+ 'ANALYST',
42
+ 'CREATIVE',
43
+ 'LONE_WOLF',
44
+ 'TEAM_PLAYER',
45
+ 'JOKESTER',
46
+ ] as const;
47
+
48
+ export type Archetype = (typeof ARCHETYPES)[number];
49
+
50
+ export const SOFT_SKILLS = [
51
+ 'COMMUNICATION',
52
+ 'CREATIVITY',
53
+ 'ANALYTICAL_THINKING',
54
+ 'PERSUASION',
55
+ 'TECHNICAL_ACUMEN',
56
+ ] as const;
57
+
58
+ export type SoftSkill = (typeof SOFT_SKILLS)[number];
59
+
60
+ export const isArchetype = (v: unknown): v is Archetype =>
61
+ typeof v === 'string' && (ARCHETYPES as readonly string[]).includes(v);
62
+
63
+ /* -------------------------------------------------------------------------- */
64
+ /* Profile */
65
+ /* -------------------------------------------------------------------------- */
66
+
67
+ export interface ActorProfile {
68
+ readonly actor: string;
69
+ readonly role: Role;
70
+ readonly archetype?: Archetype | null;
71
+ readonly soft_skills?: Readonly<Partial<Record<SoftSkill, number>>>;
72
+ readonly hard_skills?: readonly string[];
73
+ readonly model?: string | null;
74
+ /** ISO year-month, e.g. `2025-01`. */
75
+ readonly knowledge_cutoff?: string | null;
76
+ readonly mandate_alignment?: string | null;
77
+ }
78
+
79
+ export interface CapabilityIssue {
80
+ readonly dimension: 'hard_skills' | 'model' | 'knowledge_cutoff' | 'archetype' | 'mandate';
81
+ readonly severity: 'fail' | 'warn';
82
+ readonly message: string;
83
+ }
84
+
85
+ export interface CapabilityCheck {
86
+ readonly ok: boolean;
87
+ readonly issues: readonly CapabilityIssue[];
88
+ }
89
+
90
+ /* -------------------------------------------------------------------------- */
91
+ /* Model grade */
92
+ /* -------------------------------------------------------------------------- */
93
+
94
+ /**
95
+ * Does the actor's model satisfy the seat's requirement?
96
+ *
97
+ * Deliberately an exact-match check rather than a capability ranking. Ordering
98
+ * models by "smartness" would mean this module carrying an opinion about every
99
+ * vendor's lineup, going stale the week a new one ships, and silently
100
+ * downgrading a session when it guessed wrong. A Charter that wants to accept
101
+ * several models can name the one it requires and leave `preferred` advisory.
102
+ */
103
+ const modelSatisfies = (required: string | null, actual: string | null): boolean => {
104
+ if (required === null) return true;
105
+ if (actual === null) return false;
106
+ // Tolerate vendor suffixes: `claude-opus-4-8[1m]` satisfies `claude-opus-4-8`.
107
+ return actual === required || actual.startsWith(`${required}[`);
108
+ };
109
+
110
+ /** Is the actor's knowledge cutoff at or after the seat's minimum? */
111
+ const cutoffSatisfies = (minimum: string | null, actual: string | null): boolean => {
112
+ if (minimum === null) return true;
113
+ if (actual === null) return false;
114
+ return actual >= minimum;
115
+ };
116
+
117
+ /* -------------------------------------------------------------------------- */
118
+ /* Check */
119
+ /* -------------------------------------------------------------------------- */
120
+
121
+ /**
122
+ * Check an actor against the constituency it would be seated in.
123
+ *
124
+ * Hard failures block the seating; warnings are recorded and proceed. The split
125
+ * follows the architecture: the engine "validates model compliance at Writ Drop
126
+ * and refuses to spawn a mismatched model", while a preferred-but-absent model
127
+ * or an unstated archetype are configuration gaps, not disqualifications.
128
+ */
129
+ export const checkCapability = (
130
+ profile: ActorProfile,
131
+ constituency: Constituency,
132
+ ): CapabilityCheck => {
133
+ const issues: CapabilityIssue[] = [];
134
+ const held = new Set(profile.hard_skills ?? []);
135
+
136
+ for (const required of constituency.hard_skills.required) {
137
+ if (!held.has(required)) {
138
+ issues.push({
139
+ dimension: 'hard_skills',
140
+ severity: 'fail',
141
+ message: `missing required hard skill: ${required}`,
142
+ });
143
+ }
144
+ }
145
+
146
+ for (const excluded of constituency.hard_skills.excluded) {
147
+ if (held.has(excluded)) {
148
+ // Not a warning: an actor working an excluded domain produces confident
149
+ // wrong output, which is worse than producing nothing.
150
+ issues.push({
151
+ dimension: 'hard_skills',
152
+ severity: 'fail',
153
+ message: `holds excluded skill: ${excluded} — this seat excludes that domain`,
154
+ });
155
+ }
156
+ }
157
+
158
+ if (!modelSatisfies(constituency.model.required, profile.model ?? null)) {
159
+ issues.push({
160
+ dimension: 'model',
161
+ severity: 'fail',
162
+ message: `model ${profile.model ?? 'unknown'} does not satisfy required ${constituency.model.required}`,
163
+ });
164
+ }
165
+
166
+ if (
167
+ constituency.model.preferred !== null &&
168
+ profile.model !== null &&
169
+ profile.model !== undefined &&
170
+ !modelSatisfies(constituency.model.preferred, profile.model)
171
+ ) {
172
+ issues.push({
173
+ dimension: 'model',
174
+ severity: 'warn',
175
+ message: `model ${profile.model} is not the preferred ${constituency.model.preferred}`,
176
+ });
177
+ }
178
+
179
+ if (!cutoffSatisfies(constituency.model.knowledge_cutoff_minimum, profile.knowledge_cutoff ?? null)) {
180
+ issues.push({
181
+ dimension: 'knowledge_cutoff',
182
+ severity: 'fail',
183
+ message: `knowledge cutoff ${profile.knowledge_cutoff ?? 'unknown'} is before the required ${constituency.model.knowledge_cutoff_minimum}`,
184
+ });
185
+ }
186
+
187
+ if (
188
+ constituency.mandate_alignment !== null &&
189
+ profile.mandate_alignment !== constituency.mandate_alignment
190
+ ) {
191
+ issues.push({
192
+ dimension: 'mandate',
193
+ severity: 'warn',
194
+ message: `mandate ${profile.mandate_alignment ?? 'unstated'} does not match the seat's ${constituency.mandate_alignment}`,
195
+ });
196
+ }
197
+
198
+ if (profile.archetype !== undefined && profile.archetype !== null && !isArchetype(profile.archetype)) {
199
+ issues.push({
200
+ dimension: 'archetype',
201
+ severity: 'warn',
202
+ message: `unknown archetype: ${String(profile.archetype)}`,
203
+ });
204
+ }
205
+
206
+ return { ok: !issues.some((i) => i.severity === 'fail'), issues };
207
+ };
208
+
209
+ /* -------------------------------------------------------------------------- */
210
+ /* Skill mismatch */
211
+ /* -------------------------------------------------------------------------- */
212
+
213
+ export interface MismatchInput {
214
+ readonly at: string;
215
+ readonly actor: string;
216
+ readonly role: Role;
217
+ /** What the actor produced that fell outside its competence. */
218
+ readonly observed: string;
219
+ readonly excluded_domain: string;
220
+ /** Charter setting for this constituency. */
221
+ readonly auto_demotion: boolean;
222
+ }
223
+
224
+ /**
225
+ * Record a SKILL_MISMATCH fault.
226
+ *
227
+ * Fires when Hansard performance reveals a mismatch that Writ Drop did not
228
+ * catch — output in an excluded domain, or repeated failure on required domain
229
+ * work. Consequence depends on the Charter: auto-demote where permitted,
230
+ * otherwise escalate to the Speaker.
231
+ *
232
+ * The record is permanent either way. Future sessions plan against it, which is
233
+ * the point of keeping actor performance history at all.
234
+ */
235
+ export const skillMismatch = (input: MismatchInput): HansardEntry => ({
236
+ type: 'FAULT_ACTOR',
237
+ at: input.at,
238
+ actor: 'RECORD',
239
+ role: 'OPERATOR',
240
+ fields: {
241
+ Subtype: 'SKILL_MISMATCH',
242
+ 'Actor affected': input.actor,
243
+ 'Seat held': input.role,
244
+ 'Excluded domain': input.excluded_domain,
245
+ Observed: input.observed,
246
+ Consequence: input.auto_demotion
247
+ ? 'auto-demotion (permitted by Charter)'
248
+ : 'escalated to the Speaker — auto_demotion is false',
249
+ Record: 'permanent — future sessions plan against this',
250
+ },
251
+ });
252
+
253
+ /* -------------------------------------------------------------------------- */
254
+ /* Composition */
255
+ /* -------------------------------------------------------------------------- */
256
+
257
+ export interface Composition {
258
+ readonly archetypes: Readonly<Partial<Record<Archetype, number>>>;
259
+ readonly distinct: number;
260
+ readonly homogeneous: boolean;
261
+ }
262
+
263
+ /**
264
+ * Describe the archetype composition of a seated bench.
265
+ *
266
+ * Measured, never judged. RESEARCH.md asks whether complementary composition
267
+ * outperforms homogeneous composition — that is an open empirical question, and
268
+ * a framework that warned about "bad" mixes would be answering it by assumption
269
+ * and contaminating its own instrument.
270
+ */
271
+ export const composition = (profiles: readonly ActorProfile[]): Composition => {
272
+ const archetypes: Partial<Record<Archetype, number>> = {};
273
+ for (const p of profiles) {
274
+ if (p.archetype === undefined || p.archetype === null) continue;
275
+ archetypes[p.archetype] = (archetypes[p.archetype] ?? 0) + 1;
276
+ }
277
+ const distinct = Object.keys(archetypes).length;
278
+ return {
279
+ archetypes,
280
+ distinct,
281
+ homogeneous: distinct === 1 && profiles.length > 1,
282
+ };
283
+ };
284
+
285
+ /* -------------------------------------------------------------------------- */
286
+ /* Loading */
287
+ /* -------------------------------------------------------------------------- */
288
+
289
+ const isObject = (v: unknown): v is Record<string, unknown> =>
290
+ typeof v === 'object' && v !== null && !Array.isArray(v);
291
+
292
+ const strings = (v: unknown): string[] =>
293
+ Array.isArray(v) ? v.filter((x): x is string => typeof x === 'string') : [];
294
+
295
+ /**
296
+ * Parse an actor profile from `actors/<handle>.md`.
297
+ *
298
+ * Same shape as a Charter: YAML frontmatter the engine reads, prose the humans
299
+ * read. An actor file with no frontmatter is not an error — it is a profile
300
+ * that declares nothing, which a Charter requiring nothing will accept.
301
+ */
302
+ export const parseProfile = (source: string, fallbackActor: string): ActorProfile | null => {
303
+ const split = splitFrontmatter(source);
304
+ if (split === null) return null;
305
+
306
+ let raw: unknown;
307
+ try {
308
+ raw = parseYaml(split.yaml);
309
+ } catch {
310
+ return null;
311
+ }
312
+ if (!isObject(raw)) return null;
313
+
314
+ const model = isObject(raw['model']) ? raw['model'] : {};
315
+ const soft: Partial<Record<SoftSkill, number>> = {};
316
+ if (isObject(raw['soft_skills'])) {
317
+ for (const [key, value] of Object.entries(raw['soft_skills'])) {
318
+ if ((SOFT_SKILLS as readonly string[]).includes(key) && typeof value === 'number') {
319
+ soft[key as SoftSkill] = value;
320
+ }
321
+ }
322
+ }
323
+
324
+ return {
325
+ actor: typeof raw['actor'] === 'string' ? raw['actor'] : fallbackActor,
326
+ role: (typeof raw['role'] === 'string' ? raw['role'] : 'MEMBER') as Role,
327
+ archetype: isArchetype(raw['archetype']) ? raw['archetype'] : null,
328
+ soft_skills: soft,
329
+ hard_skills: strings(raw['hard_skills']),
330
+ model: typeof model['name'] === 'string' ? model['name'] : (typeof raw['model'] === 'string' ? raw['model'] : null),
331
+ knowledge_cutoff:
332
+ typeof model['knowledge_cutoff'] === 'string' ? model['knowledge_cutoff'] : null,
333
+ mandate_alignment:
334
+ typeof raw['mandate_alignment'] === 'string' ? raw['mandate_alignment'] : null,
335
+ };
336
+ };