@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.
- package/EXECUTION.md +766 -0
- package/LICENSE +21 -0
- package/POLITIK-ARCHITECTURE.md +676 -0
- package/PROTOCOLS.md +1106 -0
- package/README.md +193 -0
- package/RESEARCH.md +475 -0
- package/RUNTIME.md +1355 -0
- package/bin/politik.js +31 -0
- package/docs/adr/0001-chamber-transport.md +67 -0
- package/docs/adr/0002-charter-schema.md +142 -0
- package/docs/adr/0003-state-json-schema.md +134 -0
- package/docs/adr/0004-motion-enactment.md +215 -0
- package/docs/adr/0005-hansard-format.md +171 -0
- package/docs/adr/0006-protocol-behavioral-enforcement.md +89 -0
- package/docs/adr/0007-protocol-mechanics.md +100 -0
- package/docs/adr/0008-session-storage.md +108 -0
- package/docs/adr/0009-match-primitive.md +74 -0
- package/package.json +55 -0
- package/protocols/CONTRIBUTING.md +130 -0
- package/protocols/adversarial-collaboration.yml +28 -0
- package/protocols/corporate.yml +35 -0
- package/protocols/elimination-tournament.yml +41 -0
- package/protocols/emergency-response-ics.yml +27 -0
- package/protocols/jury-deliberation.yml +32 -0
- package/protocols/monarchy.yml +31 -0
- package/protocols/parliamentary.yml +66 -0
- package/protocols/peer-review.yml +44 -0
- package/protocols/republic.yml +36 -0
- package/protocols/socialism-collective.yml +31 -0
- package/protocols/solo.yml +32 -0
- package/src/actors.ts +742 -0
- package/src/agents.ts +318 -0
- package/src/canon.ts +173 -0
- package/src/capability.ts +336 -0
- package/src/charter.ts +545 -0
- package/src/cli.ts +2174 -0
- package/src/conflict.ts +211 -0
- package/src/containment.ts +155 -0
- package/src/crisis.ts +381 -0
- package/src/division.ts +609 -0
- package/src/doctor.ts +168 -0
- package/src/election.ts +233 -0
- package/src/envelope.ts +223 -0
- package/src/escalation.ts +320 -0
- package/src/fault.ts +241 -0
- package/src/federation.ts +299 -0
- package/src/git.ts +163 -0
- package/src/hansard.ts +201 -0
- package/src/heartbeat.ts +282 -0
- package/src/index.ts +43 -0
- package/src/init.ts +396 -0
- package/src/integrity.ts +153 -0
- package/src/ledger.ts +341 -0
- package/src/lockfs.ts +58 -0
- package/src/projection.ts +293 -0
- package/src/prorogation.ts +307 -0
- package/src/protocol-sdk.ts +248 -0
- package/src/protocol.ts +241 -0
- package/src/providers/github.ts +347 -0
- package/src/quorum.ts +137 -0
- package/src/runner.ts +534 -0
- package/src/scm.ts +142 -0
- package/src/spawn.ts +160 -0
- package/src/standings.ts +143 -0
- package/src/state.ts +179 -0
- package/src/templates/parliamentary.ts +359 -0
- package/src/tournament.ts +200 -0
- package/src/transport.ts +190 -0
- package/src/tree.ts +371 -0
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Prorogation — permanent close of a proceeding.
|
|
3
|
+
*
|
|
4
|
+
* Prorogued means the Hansard is sealed and the repo archived
|
|
5
|
+
* (POLITIK-ARCHITECTURE.md § Session States). It is terminal: unlike a
|
|
6
|
+
* SUSPENDED session, a prorogued one does not resume.
|
|
7
|
+
*
|
|
8
|
+
* Four triggers end a proceeding, first-one-wins (RUNTIME.md § Session
|
|
9
|
+
* Termination Conditions):
|
|
10
|
+
*
|
|
11
|
+
* TERMINATE if (motions >= max_motions) -> TRIGGER_TURNS
|
|
12
|
+
* OR (now >= deadline) -> TRIGGER_DEADLINE
|
|
13
|
+
* OR (total_cost >= max_cost_usd) -> TRIGGER_COST
|
|
14
|
+
* OR (AUTHORITY issues DISSOLVE) -> TRIGGER_SPEAKER
|
|
15
|
+
*
|
|
16
|
+
* The SESSION_TIMEOUT record "always names which condition triggered and the
|
|
17
|
+
* state of all other conditions at that moment", so this module evaluates every
|
|
18
|
+
* condition even once one has fired.
|
|
19
|
+
*
|
|
20
|
+
* Attribution: Steve Krisjanovs, Cordfuse
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import type { Role } from './canon.ts';
|
|
24
|
+
import { appendEntry } from './hansard.ts';
|
|
25
|
+
import type { FileWrite } from './scm.ts';
|
|
26
|
+
import { createState, type SessionStateFile } from './state.ts';
|
|
27
|
+
import { seats } from './actors.ts';
|
|
28
|
+
|
|
29
|
+
/* -------------------------------------------------------------------------- */
|
|
30
|
+
/* Triggers */
|
|
31
|
+
/* -------------------------------------------------------------------------- */
|
|
32
|
+
|
|
33
|
+
export const TRIGGERS = [
|
|
34
|
+
'TRIGGER_TURNS',
|
|
35
|
+
'TRIGGER_DEADLINE',
|
|
36
|
+
'TRIGGER_COST',
|
|
37
|
+
'TRIGGER_SPEAKER',
|
|
38
|
+
'TRIGGER_LAST_STANDING',
|
|
39
|
+
'TRIGGER_VERDICT',
|
|
40
|
+
] as const;
|
|
41
|
+
|
|
42
|
+
export type Trigger = (typeof TRIGGERS)[number];
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* When a session ends on its own — the third protocol override point (ADR-0007).
|
|
46
|
+
* `objective` (default) ends only on an explicit prorogation or a configured
|
|
47
|
+
* ceiling. `last-standing` ends the moment one seated actor remains — the Battle
|
|
48
|
+
* Royale terminal, checked after an elimination cull. `verdict` ends the moment a
|
|
49
|
+
* Division carries — a decision has been reached, so the Jury rises. Composed
|
|
50
|
+
* with `resolution: unanimity` it is a jury that deliberates until unanimous.
|
|
51
|
+
*/
|
|
52
|
+
export type TerminationPolicy = 'objective' | 'last-standing' | 'verdict';
|
|
53
|
+
|
|
54
|
+
export const TERMINATION_POLICIES: readonly TerminationPolicy[] = Object.freeze([
|
|
55
|
+
'objective',
|
|
56
|
+
'last-standing',
|
|
57
|
+
'verdict',
|
|
58
|
+
]);
|
|
59
|
+
|
|
60
|
+
export const isTerminationPolicy = (v: unknown): v is TerminationPolicy =>
|
|
61
|
+
typeof v === 'string' && (TERMINATION_POLICIES as readonly string[]).includes(v);
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Is the field down to a single survivor? Reads the seat register — AUTHORITY
|
|
65
|
+
* holds no seat, so a lone Speaker never counts as the winner. Caller should
|
|
66
|
+
* only consult this after an elimination actually occurred, so an as-yet-unseated
|
|
67
|
+
* fresh session is not mistaken for a finished one.
|
|
68
|
+
*/
|
|
69
|
+
export const lastStanding = (hansard: string): { readonly terminate: boolean; readonly survivor: string | null } => {
|
|
70
|
+
const remaining = seats(hansard);
|
|
71
|
+
return remaining.length <= 1
|
|
72
|
+
? { terminate: true, survivor: remaining[0]?.actor ?? null }
|
|
73
|
+
: { terminate: false, survivor: null };
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
/** Ceilings from the Charter. Null means the condition is not configured. */
|
|
77
|
+
export interface Ceilings {
|
|
78
|
+
readonly max_motions: number | null;
|
|
79
|
+
readonly deadline: string | null;
|
|
80
|
+
readonly max_cost_usd: number | null;
|
|
81
|
+
/** Speaker is warned before the cost ceiling — the low-fuel light. */
|
|
82
|
+
readonly cost_warning_usd?: number | null;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Measured session totals at the moment of evaluation. */
|
|
86
|
+
export interface SessionTotals {
|
|
87
|
+
readonly motions: number;
|
|
88
|
+
readonly total_cost_usd: number;
|
|
89
|
+
readonly now: string;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export interface ConditionState {
|
|
93
|
+
readonly trigger: Trigger;
|
|
94
|
+
/** False when the ceiling is not configured. */
|
|
95
|
+
readonly configured: boolean;
|
|
96
|
+
readonly fired: boolean;
|
|
97
|
+
/** Human-readable current-vs-ceiling, for the SESSION_TIMEOUT record. */
|
|
98
|
+
readonly detail: string;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export interface TerminationCheck {
|
|
102
|
+
readonly terminate: boolean;
|
|
103
|
+
/** The condition that fired first, in evaluation order. Null if none did. */
|
|
104
|
+
readonly trigger: Trigger | null;
|
|
105
|
+
/** Every condition's state at this moment, fired or not. */
|
|
106
|
+
readonly conditions: readonly ConditionState[];
|
|
107
|
+
/** True when cost has passed the warning line but not the ceiling. */
|
|
108
|
+
readonly cost_warning: boolean;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/* -------------------------------------------------------------------------- */
|
|
112
|
+
/* Evaluation */
|
|
113
|
+
/* -------------------------------------------------------------------------- */
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Evaluate all termination conditions.
|
|
117
|
+
*
|
|
118
|
+
* `dissolve` represents the AUTHORITY issuing the DISSOLVE verb; it is a
|
|
119
|
+
* deliberate act rather than a measurement, so the caller supplies it.
|
|
120
|
+
*
|
|
121
|
+
* An unconfigured ceiling never fires. A session with no ceilings and no
|
|
122
|
+
* DISSOLVE runs until something else stops it, which is a legitimate
|
|
123
|
+
* configuration and not an error.
|
|
124
|
+
*/
|
|
125
|
+
export const checkTermination = (
|
|
126
|
+
ceilings: Ceilings,
|
|
127
|
+
totals: SessionTotals,
|
|
128
|
+
dissolve = false,
|
|
129
|
+
): TerminationCheck => {
|
|
130
|
+
const conditions: ConditionState[] = [];
|
|
131
|
+
|
|
132
|
+
const turnsConfigured = ceilings.max_motions !== null;
|
|
133
|
+
const turnsFired = turnsConfigured && totals.motions >= (ceilings.max_motions ?? 0);
|
|
134
|
+
conditions.push({
|
|
135
|
+
trigger: 'TRIGGER_TURNS',
|
|
136
|
+
configured: turnsConfigured,
|
|
137
|
+
fired: turnsFired,
|
|
138
|
+
detail: turnsConfigured
|
|
139
|
+
? `${totals.motions} of ${ceilings.max_motions} motions`
|
|
140
|
+
: 'not configured',
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
const deadlineConfigured = ceilings.deadline !== null;
|
|
144
|
+
const deadlineMs = deadlineConfigured ? Date.parse(ceilings.deadline ?? '') : NaN;
|
|
145
|
+
const nowMs = Date.parse(totals.now);
|
|
146
|
+
const deadlineFired =
|
|
147
|
+
deadlineConfigured &&
|
|
148
|
+
!Number.isNaN(deadlineMs) &&
|
|
149
|
+
!Number.isNaN(nowMs) &&
|
|
150
|
+
nowMs >= deadlineMs;
|
|
151
|
+
conditions.push({
|
|
152
|
+
trigger: 'TRIGGER_DEADLINE',
|
|
153
|
+
configured: deadlineConfigured,
|
|
154
|
+
fired: deadlineFired,
|
|
155
|
+
detail: deadlineConfigured ? `${totals.now} vs ${ceilings.deadline}` : 'not configured',
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
const costConfigured = ceilings.max_cost_usd !== null;
|
|
159
|
+
const costFired = costConfigured && totals.total_cost_usd >= (ceilings.max_cost_usd ?? 0);
|
|
160
|
+
conditions.push({
|
|
161
|
+
trigger: 'TRIGGER_COST',
|
|
162
|
+
configured: costConfigured,
|
|
163
|
+
fired: costFired,
|
|
164
|
+
detail: costConfigured
|
|
165
|
+
? `$${totals.total_cost_usd.toFixed(4)} of $${(ceilings.max_cost_usd ?? 0).toFixed(2)}`
|
|
166
|
+
: 'not configured',
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
conditions.push({
|
|
170
|
+
trigger: 'TRIGGER_SPEAKER',
|
|
171
|
+
configured: true,
|
|
172
|
+
fired: dissolve,
|
|
173
|
+
detail: dissolve ? 'DISSOLVE issued by AUTHORITY' : 'no DISSOLVE issued',
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
const first = conditions.find((c) => c.fired) ?? null;
|
|
177
|
+
|
|
178
|
+
const warning = ceilings.cost_warning_usd ?? null;
|
|
179
|
+
const cost_warning =
|
|
180
|
+
warning !== null && !costFired && totals.total_cost_usd >= warning;
|
|
181
|
+
|
|
182
|
+
return {
|
|
183
|
+
terminate: first !== null,
|
|
184
|
+
trigger: first?.trigger ?? null,
|
|
185
|
+
conditions,
|
|
186
|
+
cost_warning,
|
|
187
|
+
};
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
/* -------------------------------------------------------------------------- */
|
|
191
|
+
/* Prorogation */
|
|
192
|
+
/* -------------------------------------------------------------------------- */
|
|
193
|
+
|
|
194
|
+
export class ProrogationError extends Error {
|
|
195
|
+
constructor(message: string) {
|
|
196
|
+
super(message);
|
|
197
|
+
this.name = 'ProrogationError';
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
export interface ProrogationInput {
|
|
202
|
+
readonly at: string;
|
|
203
|
+
readonly actor: string;
|
|
204
|
+
readonly role: Role;
|
|
205
|
+
readonly trigger: Trigger;
|
|
206
|
+
readonly state: SessionStateFile;
|
|
207
|
+
/** All conditions at the moment of termination, for the record. */
|
|
208
|
+
readonly check?: TerminationCheck;
|
|
209
|
+
/** Decisions summary auto-committed back to the parent layer. */
|
|
210
|
+
readonly summary?: string;
|
|
211
|
+
/** Milestone to close, if the session used one. */
|
|
212
|
+
readonly milestone_id?: string | null;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
export interface ProrogationResult {
|
|
216
|
+
readonly state: SessionStateFile;
|
|
217
|
+
readonly files: readonly FileWrite[];
|
|
218
|
+
readonly hansard: string;
|
|
219
|
+
/** Milestone the caller must close via ScmProvider. Null if none. */
|
|
220
|
+
readonly milestone_to_close: string | null;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Prorogue a proceeding.
|
|
225
|
+
*
|
|
226
|
+
* Constraints enforced:
|
|
227
|
+
* - A TRIGGER_SPEAKER prorogation requires AUTHORITY. DISSOLVE is an
|
|
228
|
+
* AUTHORITY verb; the automatic triggers are the engine's and need no actor
|
|
229
|
+
* authority.
|
|
230
|
+
* - An already-prorogued session cannot be prorogued twice — the Hansard is
|
|
231
|
+
* sealed, and a second seal would be a post-seal write.
|
|
232
|
+
* - A session in CONSTITUTIONAL_CRISIS cannot be prorogued. RUNTIME.md is
|
|
233
|
+
* explicit that a crisis suspends rather than prorogues, precisely so a
|
|
234
|
+
* captured AUTHORITY cannot dissolve the session before evidence
|
|
235
|
+
* accumulates. The substrate must survive a premature dissolution.
|
|
236
|
+
*/
|
|
237
|
+
export const prorogue = (
|
|
238
|
+
input: ProrogationInput,
|
|
239
|
+
hansard: string,
|
|
240
|
+
): ProrogationResult => {
|
|
241
|
+
if (input.state.state === 'PROROGUED') {
|
|
242
|
+
throw new ProrogationError('session is already prorogued — the Hansard is sealed');
|
|
243
|
+
}
|
|
244
|
+
if (input.trigger === 'TRIGGER_SPEAKER' && input.role !== 'AUTHORITY') {
|
|
245
|
+
throw new ProrogationError(
|
|
246
|
+
`DISSOLVE is an AUTHORITY verb — ${input.actor} is ${input.role}`,
|
|
247
|
+
);
|
|
248
|
+
}
|
|
249
|
+
if (input.state.suspension?.cause === 'CONSTITUTIONAL_CRISIS') {
|
|
250
|
+
throw new ProrogationError(
|
|
251
|
+
'a session in CONSTITUTIONAL_CRISIS cannot be prorogued — it suspends pending external review',
|
|
252
|
+
);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
const state = createState({
|
|
256
|
+
session_guid: input.state.session_guid,
|
|
257
|
+
protocol: input.state.protocol,
|
|
258
|
+
state: 'PROROGUED',
|
|
259
|
+
quorum: input.state.quorum,
|
|
260
|
+
hansard_head: input.state.hansard_head,
|
|
261
|
+
updated_at: input.at,
|
|
262
|
+
suspension: null,
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
const fields: Record<string, string> = {
|
|
266
|
+
Trigger: input.trigger,
|
|
267
|
+
'Session status': 'PROROGUED — Hansard sealed',
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
// The record names every condition, not just the one that fired.
|
|
271
|
+
for (const condition of input.check?.conditions ?? []) {
|
|
272
|
+
fields[condition.trigger] = `${condition.fired ? 'FIRED' : 'not met'} — ${condition.detail}`;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
const files: FileWrite[] = [];
|
|
276
|
+
if (input.summary !== undefined && input.summary.trim() !== '') {
|
|
277
|
+
files.push({
|
|
278
|
+
path: 'SUMMARY.md',
|
|
279
|
+
content: [
|
|
280
|
+
'# SESSION SUMMARY',
|
|
281
|
+
'',
|
|
282
|
+
`**Session:** ${input.state.session_guid}`,
|
|
283
|
+
`**Prorogued:** ${input.at}`,
|
|
284
|
+
`**Trigger:** ${input.trigger}`,
|
|
285
|
+
'',
|
|
286
|
+
'---',
|
|
287
|
+
'',
|
|
288
|
+
input.summary.trim(),
|
|
289
|
+
'',
|
|
290
|
+
].join('\n'),
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
return {
|
|
295
|
+
state,
|
|
296
|
+
files,
|
|
297
|
+
hansard: appendEntry(hansard, {
|
|
298
|
+
type: 'SESSION_TIMEOUT',
|
|
299
|
+
at: input.at,
|
|
300
|
+
actor: input.actor,
|
|
301
|
+
role: input.role,
|
|
302
|
+
fields,
|
|
303
|
+
body: input.summary?.trim(),
|
|
304
|
+
}),
|
|
305
|
+
milestone_to_close: input.milestone_id ?? null,
|
|
306
|
+
};
|
|
307
|
+
};
|
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Protocol SDK — lint, generate, and discover protocols.
|
|
3
|
+
*
|
|
4
|
+
* `parseProtocol` answers "is this manifest loadable?". The linter answers a
|
|
5
|
+
* different and stricter question: "is this manifest *coherent*?" — does it
|
|
6
|
+
* translate terms CANON actually has, are its behavioural flags consistent with
|
|
7
|
+
* its declared mode, and will a Charter written against it survive Writ Drop.
|
|
8
|
+
*
|
|
9
|
+
* A loadable manifest can still be incoherent. `mode: darwinist` with
|
|
10
|
+
* `no_escalation: false` loads cleanly and then produces a session that accepts
|
|
11
|
+
* appeals a Darwinist protocol has no mechanism to resolve.
|
|
12
|
+
*
|
|
13
|
+
* Attribution: Steve Krisjanovs, Cordfuse
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { PRIMITIVES, ROLES, VERBS, type Role } from './canon.ts';
|
|
17
|
+
import {
|
|
18
|
+
parseProtocol,
|
|
19
|
+
type Protocol,
|
|
20
|
+
type ProtocolMode,
|
|
21
|
+
} from './protocol.ts';
|
|
22
|
+
|
|
23
|
+
/* -------------------------------------------------------------------------- */
|
|
24
|
+
/* Lint */
|
|
25
|
+
/* -------------------------------------------------------------------------- */
|
|
26
|
+
|
|
27
|
+
export type LintSeverity = 'error' | 'warning';
|
|
28
|
+
|
|
29
|
+
export interface LintFinding {
|
|
30
|
+
readonly severity: LintSeverity;
|
|
31
|
+
readonly field: string;
|
|
32
|
+
readonly message: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface LintResult {
|
|
36
|
+
readonly ok: boolean;
|
|
37
|
+
readonly findings: readonly LintFinding[];
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Every CANON term a protocol may legitimately translate. */
|
|
41
|
+
const TRANSLATABLE: readonly string[] = Object.freeze([...PRIMITIVES, ...VERBS]);
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Behavioural expectations implied by each mode.
|
|
45
|
+
*
|
|
46
|
+
* These are *warnings*, not errors: the architecture describes modes as
|
|
47
|
+
* families, not as constraints, and a protocol author may have a reason. But a
|
|
48
|
+
* mismatch is nearly always a mistake, and silently shipping one produces
|
|
49
|
+
* sessions that behave unlike every sibling protocol in their family.
|
|
50
|
+
*/
|
|
51
|
+
const MODE_EXPECTATIONS: Readonly<
|
|
52
|
+
Record<ProtocolMode, { no_escalation?: boolean; no_record?: boolean; immutable_charter?: boolean }>
|
|
53
|
+
> = Object.freeze({
|
|
54
|
+
constitutional: { no_escalation: false, no_record: false },
|
|
55
|
+
authoritarian: { no_record: false },
|
|
56
|
+
darwinist: { no_record: false },
|
|
57
|
+
ephemeral: { no_record: true },
|
|
58
|
+
immutable: { immutable_charter: true },
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Lint a protocol manifest against CANON.
|
|
63
|
+
*
|
|
64
|
+
* Errors make a protocol unusable. Warnings flag likely authoring mistakes.
|
|
65
|
+
*/
|
|
66
|
+
export const lintProtocol = (protocol: Protocol): LintResult => {
|
|
67
|
+
const findings: LintFinding[] = [];
|
|
68
|
+
|
|
69
|
+
if (!/^[a-z0-9][a-z0-9-]*$/.test(protocol.name)) {
|
|
70
|
+
findings.push({
|
|
71
|
+
severity: 'error',
|
|
72
|
+
field: 'name',
|
|
73
|
+
message: 'must be lower-kebab-case — it is a filename and a Charter key',
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (!/^\d+\.\d+\.\d+$/.test(protocol.version)) {
|
|
78
|
+
findings.push({
|
|
79
|
+
severity: 'warning',
|
|
80
|
+
field: 'version',
|
|
81
|
+
message: `not semver: "${protocol.version}"`,
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Translating a term CANON does not have is always an error: it will never be
|
|
86
|
+
// looked up, so it is dead vocabulary that misleads whoever reads the file.
|
|
87
|
+
for (const key of Object.keys(protocol.primitives)) {
|
|
88
|
+
if (!TRANSLATABLE.includes(key)) {
|
|
89
|
+
findings.push({
|
|
90
|
+
severity: 'error',
|
|
91
|
+
field: `primitives.${key}`,
|
|
92
|
+
message: 'not a CANON primitive or verb — this term will never be used',
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
for (const role of ROLES) {
|
|
98
|
+
if (protocol.roles[role] === undefined) {
|
|
99
|
+
findings.push({
|
|
100
|
+
severity: 'warning',
|
|
101
|
+
field: `roles.${role}`,
|
|
102
|
+
message: 'unmapped — falls back to the CANON term',
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// Duplicate role terms make the interface ambiguous: two constituencies would
|
|
108
|
+
// render identically and a human could not tell which one acted.
|
|
109
|
+
const seen = new Map<string, Role>();
|
|
110
|
+
for (const role of ROLES) {
|
|
111
|
+
const label = protocol.roles[role];
|
|
112
|
+
if (label === undefined) continue;
|
|
113
|
+
const prior = seen.get(label.toLowerCase());
|
|
114
|
+
if (prior !== undefined) {
|
|
115
|
+
findings.push({
|
|
116
|
+
severity: 'error',
|
|
117
|
+
field: `roles.${role}`,
|
|
118
|
+
message: `duplicate term "${label}" — already maps ${prior}`,
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
seen.set(label.toLowerCase(), role);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const expectations = MODE_EXPECTATIONS[protocol.mode];
|
|
125
|
+
for (const [flag, expected] of Object.entries(expectations)) {
|
|
126
|
+
const actual = protocol[flag as 'no_escalation' | 'no_record' | 'immutable_charter'];
|
|
127
|
+
if (actual !== expected) {
|
|
128
|
+
findings.push({
|
|
129
|
+
severity: 'warning',
|
|
130
|
+
field: flag,
|
|
131
|
+
message: `${protocol.mode} protocols normally set ${flag}: ${String(expected)}`,
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// A recording protocol whose record evaporates is self-contradictory.
|
|
137
|
+
if (protocol.record_mode === 'ephemeral' && !protocol.no_record) {
|
|
138
|
+
findings.push({
|
|
139
|
+
severity: 'error',
|
|
140
|
+
field: 'record_mode',
|
|
141
|
+
message: 'ephemeral record_mode requires no_record: true — the record cannot both persist and not',
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
if (protocol.no_record && protocol.record_mode !== 'ephemeral') {
|
|
146
|
+
findings.push({
|
|
147
|
+
severity: 'warning',
|
|
148
|
+
field: 'no_record',
|
|
149
|
+
message: `no_record: true with record_mode: ${protocol.record_mode} — expected ephemeral`,
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// AUTHORITY holding a domain veto is redundant: AUTHORITY already holds VETO
|
|
154
|
+
// as a constitutional power. Declaring it says less than it appears to.
|
|
155
|
+
if (protocol.domain_veto.includes('AUTHORITY')) {
|
|
156
|
+
findings.push({
|
|
157
|
+
severity: 'warning',
|
|
158
|
+
field: 'domain_veto',
|
|
159
|
+
message: 'AUTHORITY already holds VETO — a domain veto adds nothing',
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
return { ok: !findings.some((f) => f.severity === 'error'), findings };
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
/** Parse and lint in one step. */
|
|
167
|
+
export const lintSource = (source: string): LintResult => {
|
|
168
|
+
const parsed = parseProtocol(source);
|
|
169
|
+
if (!parsed.ok) {
|
|
170
|
+
return {
|
|
171
|
+
ok: false,
|
|
172
|
+
findings: parsed.issues.map((i) => ({
|
|
173
|
+
severity: 'error' as const,
|
|
174
|
+
field: i.field,
|
|
175
|
+
message: i.message,
|
|
176
|
+
})),
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
const result = lintProtocol(parsed.protocol);
|
|
180
|
+
return {
|
|
181
|
+
ok: result.ok,
|
|
182
|
+
findings: [
|
|
183
|
+
...parsed.warnings.map((w) => ({
|
|
184
|
+
severity: 'warning' as const,
|
|
185
|
+
field: w.field,
|
|
186
|
+
message: w.message,
|
|
187
|
+
})),
|
|
188
|
+
...result.findings.filter((f) => !f.field.startsWith('roles.')),
|
|
189
|
+
],
|
|
190
|
+
};
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
/* -------------------------------------------------------------------------- */
|
|
194
|
+
/* Generate */
|
|
195
|
+
/* -------------------------------------------------------------------------- */
|
|
196
|
+
|
|
197
|
+
export interface GenerateOptions {
|
|
198
|
+
readonly name: string;
|
|
199
|
+
readonly mode?: ProtocolMode;
|
|
200
|
+
readonly roles?: Readonly<Partial<Record<Role, string>>>;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Generate a protocol manifest skeleton.
|
|
205
|
+
*
|
|
206
|
+
* Every CANON role and primitive appears, mapped to itself where the author has
|
|
207
|
+
* not supplied a term. That makes the full translatable surface visible in the
|
|
208
|
+
* file rather than something to be discovered from documentation.
|
|
209
|
+
*/
|
|
210
|
+
export const generateProtocol = (options: GenerateOptions): string => {
|
|
211
|
+
const mode = options.mode ?? 'constitutional';
|
|
212
|
+
const expectations = MODE_EXPECTATIONS[mode];
|
|
213
|
+
|
|
214
|
+
const roleLines = ROLES.map(
|
|
215
|
+
(role) => ` ${role}: ${options.roles?.[role] ?? role}`,
|
|
216
|
+
).join('\n');
|
|
217
|
+
|
|
218
|
+
const primitiveLines = PRIMITIVES.map((p) => ` ${p}: ${p}`).join('\n');
|
|
219
|
+
const verbLines = VERBS.map((v) => ` ${v}: ${v}`).join('\n');
|
|
220
|
+
|
|
221
|
+
return `# ${options.name} — protocol manifest.
|
|
222
|
+
#
|
|
223
|
+
# Generated skeleton. Replace the right-hand side of each mapping with your
|
|
224
|
+
# domain's vocabulary; delete any line you want to fall back to the CANON term.
|
|
225
|
+
#
|
|
226
|
+
# Lint before shipping: politik protocol lint protocols/${options.name}.yml
|
|
227
|
+
|
|
228
|
+
protocol:
|
|
229
|
+
name: ${options.name}
|
|
230
|
+
version: 0.1.0
|
|
231
|
+
mode: ${mode}
|
|
232
|
+
record_mode: anchored
|
|
233
|
+
|
|
234
|
+
roles:
|
|
235
|
+
${roleLines}
|
|
236
|
+
|
|
237
|
+
primitives:
|
|
238
|
+
${primitiveLines}
|
|
239
|
+
|
|
240
|
+
# Verbs
|
|
241
|
+
${verbLines}
|
|
242
|
+
|
|
243
|
+
domain_veto: []
|
|
244
|
+
no_escalation: ${expectations.no_escalation ?? false}
|
|
245
|
+
no_record: ${expectations.no_record ?? false}
|
|
246
|
+
immutable_charter: ${expectations.immutable_charter ?? false}
|
|
247
|
+
`;
|
|
248
|
+
};
|