@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,320 @@
1
+ /**
2
+ * Point of Order — escalation workflow.
3
+ *
4
+ * The flow (RUNTIME.md § POINT OF ORDER — ESCALATION FLOW):
5
+ *
6
+ * Agent hits a decision requiring human judgement
7
+ * -> commits escalation to escalations/[timestamp].md
8
+ * -> Speaker notified
9
+ * -> session pauses
10
+ * -> Speaker commits ruling
11
+ * -> session resumes
12
+ * -> ruling recorded permanently in Hansard
13
+ *
14
+ * Pure, like the initializer: this module produces the files and the next state,
15
+ * and the caller commits them. Notification is the caller's job too — it is an
16
+ * ScmProvider concern, and keeping it out means the workflow can be exercised
17
+ * with no network.
18
+ *
19
+ * Note on vocabulary: RUNTIME.md predates ADR-0003 and describes the pause as
20
+ * `STATE.json → paused: true`. ADR-0003 supersedes that mapping: the session
21
+ * becomes SUSPENDED with `suspension.cause: POINT_OF_ORDER`. This module
22
+ * implements the ADR.
23
+ *
24
+ * Attribution: Steve Krisjanovs, Cordfuse
25
+ */
26
+
27
+ import type { Role } from './canon.ts';
28
+ import { RECORD_TYPES, appendEntry, type HansardEntry } from './hansard.ts';
29
+ import type { FileWrite } from './scm.ts';
30
+ import { createState, type SessionStateFile } from './state.ts';
31
+
32
+ /* -------------------------------------------------------------------------- */
33
+ /* Filing */
34
+ /* -------------------------------------------------------------------------- */
35
+
36
+ export interface EscalationInput {
37
+ /** Sequence number within the session. Determines the ruling filename. */
38
+ readonly sequence: number;
39
+ /** ISO-8601 UTC. Also forms the escalation filename. */
40
+ readonly at: string;
41
+ readonly actor: string;
42
+ readonly role: Role;
43
+ readonly title: string;
44
+ /** What decision requires human judgement, and why the agent cannot make it. */
45
+ readonly body: string;
46
+ /** Current state, which the escalation suspends. */
47
+ readonly state: SessionStateFile;
48
+ /**
49
+ * True when the Charter's protocol declares `session.escalation: disabled`
50
+ * (ADR-0006). A protocol with no appeals — a Darwinist chamber — has no
51
+ * mechanism to resolve a Point of Order, so it refuses to raise one at all.
52
+ */
53
+ readonly escalation_disabled?: boolean;
54
+ }
55
+
56
+ export interface EscalationResult {
57
+ readonly escalation_path: string;
58
+ /** Where the Speaker must commit their ruling. */
59
+ readonly ruling_path: string;
60
+ /** SUSPENDED, cause POINT_OF_ORDER. */
61
+ readonly state: SessionStateFile;
62
+ readonly files: readonly FileWrite[];
63
+ readonly hansard_entry: HansardEntry;
64
+ /** Message body for the Speaker notification. The caller delivers it. */
65
+ readonly notification: string;
66
+ }
67
+
68
+ /** Filename-safe form of an ISO timestamp. */
69
+ const stamp = (at: string): string => at.replace(/[:]/g, '').replace(/\.\d+Z$/, 'Z');
70
+
71
+ export const escalationPath = (at: string): string => `escalations/${stamp(at)}.md`;
72
+ export const rulingPath = (sequence: number): string =>
73
+ `escalations/ruling-${String(sequence).padStart(3, '0')}.md`;
74
+
75
+ /**
76
+ * File a Point of Order.
77
+ *
78
+ * Suspends the session. Every actor reads STATE.json before acting, so agents
79
+ * halt as soon as this state is committed — the pause is a consequence of the
80
+ * record, not a separate mechanism.
81
+ */
82
+ export class EscalationError extends Error {
83
+ constructor(message: string) {
84
+ super(message);
85
+ this.name = 'EscalationError';
86
+ }
87
+ }
88
+
89
+ export const fileEscalation = (
90
+ input: EscalationInput,
91
+ hansard: string,
92
+ ): EscalationResult & { readonly hansard: string } => {
93
+ // A protocol that declares no escalation has no mechanism to resolve a Point
94
+ // of Order (ADR-0006). Refusing to raise one is what makes a Darwinist chamber
95
+ // behave like one: the decision stands, and a dissenting actor exits.
96
+ if (input.escalation_disabled === true) {
97
+ throw new EscalationError('this protocol declares no escalation — a Point of Order cannot be raised');
98
+ }
99
+
100
+ // A Point of Order suspends a live sitting, so it can only be raised from one.
101
+ // Without this guard it would fire on any state — reopening a PROROGUED
102
+ // (sealed) session, or clobbering the pending ruling of a session already
103
+ // SUSPENDED for an earlier Point of Order. Mirrors the Division/Assent guard.
104
+ if (input.state.state !== 'CONVENED') {
105
+ throw new EscalationError(
106
+ `cannot raise a Point of Order while the session is ${input.state.state}`,
107
+ );
108
+ }
109
+
110
+ const escalation_path = escalationPath(input.at);
111
+ const ruling_path = rulingPath(input.sequence);
112
+
113
+ const document = [
114
+ `# POINT OF ORDER — ${input.title}`,
115
+ '',
116
+ `**Filed by:** ${input.actor} (${input.role})`,
117
+ `**Filed at:** ${input.at}`,
118
+ `**Ruling expected at:** ${ruling_path}`,
119
+ '',
120
+ '---',
121
+ '',
122
+ input.body.trim(),
123
+ '',
124
+ ].join('\n');
125
+
126
+ const state = createState({
127
+ session_guid: input.state.session_guid,
128
+ protocol: input.state.protocol,
129
+ state: 'SUSPENDED',
130
+ quorum: input.state.quorum,
131
+ hansard_head: input.state.hansard_head,
132
+ updated_at: input.at,
133
+ suspension: {
134
+ cause: 'POINT_OF_ORDER',
135
+ since: input.at,
136
+ record_ref: escalation_path,
137
+ escalation_ref: escalation_path,
138
+ },
139
+ });
140
+
141
+ const hansard_entry: HansardEntry = {
142
+ type: 'POINT_OF_ORDER',
143
+ at: input.at,
144
+ actor: input.actor,
145
+ role: input.role,
146
+ fields: {
147
+ Escalation: escalation_path,
148
+ 'Ruling expected': ruling_path,
149
+ 'Session status': 'SUSPENDED — awaiting Speaker ruling',
150
+ },
151
+ body: input.title,
152
+ };
153
+
154
+ const notification = [
155
+ 'A Point of Order has been raised.',
156
+ '',
157
+ `Filed by: ${input.actor} (${input.role})`,
158
+ `Subject: ${input.title}`,
159
+ '',
160
+ input.body.trim(),
161
+ '',
162
+ `Commit your ruling to: ${ruling_path}`,
163
+ 'Session is paused pending your response.',
164
+ ].join('\n');
165
+
166
+ return {
167
+ escalation_path,
168
+ ruling_path,
169
+ state,
170
+ files: [{ path: escalation_path, content: document }],
171
+ hansard_entry,
172
+ notification,
173
+ hansard: appendEntry(hansard, hansard_entry),
174
+ };
175
+ };
176
+
177
+ /* -------------------------------------------------------------------------- */
178
+ /* Ruling */
179
+ /* -------------------------------------------------------------------------- */
180
+
181
+ export type RulingOutcome = 'UPHELD' | 'REVERSED' | 'NOTED';
182
+
183
+ export interface RulingInput {
184
+ readonly sequence: number;
185
+ readonly at: string;
186
+ /** The Speaker. Only AUTHORITY may rule. */
187
+ readonly actor: string;
188
+ readonly role: Role;
189
+ readonly outcome: RulingOutcome;
190
+ readonly body: string;
191
+ /** The escalation being ruled on. */
192
+ readonly escalation_path: string;
193
+ /** The suspended state this ruling lifts. */
194
+ readonly state: SessionStateFile;
195
+ }
196
+
197
+ export class RulingError extends Error {
198
+ constructor(message: string) {
199
+ super(message);
200
+ this.name = 'RulingError';
201
+ }
202
+ }
203
+
204
+ export interface RulingResult {
205
+ readonly ruling_path: string;
206
+ /** CONVENED — the suspension is lifted. */
207
+ readonly state: SessionStateFile;
208
+ readonly files: readonly FileWrite[];
209
+ readonly hansard: string;
210
+ }
211
+
212
+ /**
213
+ * Commit a Speaker's ruling and resume the session.
214
+ *
215
+ * Two constitutional constraints are enforced rather than assumed:
216
+ * - Only AUTHORITY may rule. A Point of Order exists precisely because a
217
+ * machine could not make the call; letting a non-Speaker resolve it would
218
+ * defeat the mechanism.
219
+ * - The session must actually be suspended for a Point of Order. Resuming a
220
+ * session suspended by a *different* cause would silently discard that cause.
221
+ */
222
+ export const commitRuling = (input: RulingInput, hansard: string): RulingResult => {
223
+ if (input.role !== 'AUTHORITY') {
224
+ throw new RulingError(
225
+ `only AUTHORITY may rule on a Point of Order — ${input.actor} is ${input.role}`,
226
+ );
227
+ }
228
+ if (input.state.state !== 'SUSPENDED') {
229
+ throw new RulingError(
230
+ `cannot rule on a session in state ${input.state.state} — expected SUSPENDED`,
231
+ );
232
+ }
233
+ if (input.state.suspension?.cause !== 'POINT_OF_ORDER') {
234
+ throw new RulingError(
235
+ `session is suspended for ${input.state.suspension?.cause ?? 'an unknown cause'}, not POINT_OF_ORDER`,
236
+ );
237
+ }
238
+
239
+ const ruling_path = rulingPath(input.sequence);
240
+
241
+ const document = [
242
+ `# RULING ${String(input.sequence).padStart(3, '0')} — ${input.outcome}`,
243
+ '',
244
+ `**Ruled by:** ${input.actor} (AUTHORITY)`,
245
+ `**Ruled at:** ${input.at}`,
246
+ `**Escalation:** ${input.escalation_path}`,
247
+ '',
248
+ '---',
249
+ '',
250
+ input.body.trim(),
251
+ '',
252
+ ].join('\n');
253
+
254
+ const state = createState({
255
+ session_guid: input.state.session_guid,
256
+ protocol: input.state.protocol,
257
+ state: 'CONVENED',
258
+ quorum: input.state.quorum,
259
+ hansard_head: input.state.hansard_head,
260
+ updated_at: input.at,
261
+ suspension: null,
262
+ });
263
+
264
+ return {
265
+ ruling_path,
266
+ state,
267
+ files: [{ path: ruling_path, content: document }],
268
+ hansard: appendEntry(hansard, {
269
+ type: 'RULING',
270
+ at: input.at,
271
+ actor: input.actor,
272
+ role: 'AUTHORITY',
273
+ fields: {
274
+ Outcome: input.outcome,
275
+ Ruling: ruling_path,
276
+ Escalation: input.escalation_path,
277
+ 'Session status': 'CONVENED — proceeding resumes',
278
+ },
279
+ body: input.body.trim(),
280
+ }),
281
+ };
282
+ };
283
+
284
+ /* -------------------------------------------------------------------------- */
285
+ /* Fault records */
286
+ /* -------------------------------------------------------------------------- */
287
+
288
+ export interface FaultInput {
289
+ readonly at: string;
290
+ readonly actor: string;
291
+ readonly role: Role;
292
+ readonly action_attempted: string;
293
+ readonly fault_type: string;
294
+ readonly fault_detail: string;
295
+ readonly actor_penalised: boolean;
296
+ readonly resolution_path: 'PATH_A' | 'PATH_B' | 'PATH_C';
297
+ }
298
+
299
+ /**
300
+ * Render a SESSION_FAULT record in the shape RUNTIME.md specifies.
301
+ * Critical faults (PATH_C) use the SESSION_FAULT_CRITICAL type.
302
+ */
303
+ export const faultEntry = (input: FaultInput): HansardEntry => ({
304
+ type:
305
+ input.resolution_path === 'PATH_C'
306
+ ? RECORD_TYPES.SESSION_FAULT_CRITICAL
307
+ : RECORD_TYPES.SESSION_FAULT,
308
+ at: input.at,
309
+ actor: input.actor,
310
+ role: input.role,
311
+ fields: {
312
+ 'Action attempted': input.action_attempted,
313
+ 'Fault type': input.fault_type,
314
+ 'Fault detail': input.fault_detail,
315
+ 'Actor penalised': input.actor_penalised
316
+ ? 'Yes'
317
+ : 'No — infrastructure fault, actor blameless',
318
+ 'Resolution path': input.resolution_path,
319
+ },
320
+ });
package/src/fault.ts ADDED
@@ -0,0 +1,241 @@
1
+ /**
2
+ * Fault classification and auto-recovery.
3
+ *
4
+ * RUNTIME.md § Infrastructure Fault Classification states the problem exactly:
5
+ * a failed action looks identical in the Hansard whether the agent made a bad
6
+ * decision or a secret expired. Without attribution an innocent actor is
7
+ * penalised — demotion triggers, performance record damaged — for something
8
+ * entirely outside their control.
9
+ *
10
+ * So every failure is classified **before it touches the actor's record**, and
11
+ * the classification decides both who is blamed and what happens next:
12
+ *
13
+ * PATH_A — transient. Retry; no human needed.
14
+ * PATH_B — a human must fix it. Suspend and escalate.
15
+ * PATH_C — irrecoverable. Prorogue.
16
+ *
17
+ * ## On classifying from stderr
18
+ *
19
+ * The signals here are heuristics over vendor error text, and they will drift.
20
+ * That is acceptable because of which way they fail: an unrecognised fault
21
+ * classifies as `FAULT_AMBIGUOUS`, which does **not** penalise the actor and
22
+ * routes to a human. The cost of being wrong is a Speaker reading an error
23
+ * message; the cost of guessing confidently would be blaming an actor for an
24
+ * outage.
25
+ *
26
+ * Attribution: Steve Krisjanovs, Cordfuse
27
+ */
28
+
29
+ import type { Role } from './canon.ts';
30
+ import { RECORD_TYPES, type HansardEntry } from './hansard.ts';
31
+ import type { SpawnResult } from './spawn.ts';
32
+
33
+ /* -------------------------------------------------------------------------- */
34
+ /* Taxonomy */
35
+ /* -------------------------------------------------------------------------- */
36
+
37
+ export const FAULT_TYPES = [
38
+ 'FAULT_ACTOR',
39
+ 'FAULT_INFRA',
40
+ 'FAULT_EXTERNAL',
41
+ 'FAULT_AMBIGUOUS',
42
+ ] as const;
43
+
44
+ export type FaultType = (typeof FAULT_TYPES)[number];
45
+
46
+ export type ResolutionPath = 'PATH_A' | 'PATH_B' | 'PATH_C';
47
+
48
+ export interface Classification {
49
+ readonly fault_type: FaultType;
50
+ readonly path: ResolutionPath;
51
+ /** Does this count against the actor's record? */
52
+ readonly actor_penalised: boolean;
53
+ /** Signal that produced the classification, for the record. */
54
+ readonly detail: string;
55
+ }
56
+
57
+ /* -------------------------------------------------------------------------- */
58
+ /* Recoverable signals */
59
+ /* -------------------------------------------------------------------------- */
60
+
61
+ export const AUTO_RECOVERABLE = ['rate_limit', 'transient_outage', 'timeout'] as const;
62
+
63
+ export type RecoverableKind = (typeof AUTO_RECOVERABLE)[number];
64
+
65
+ const SIGNALS: readonly { kind: RecoverableKind; pattern: RegExp }[] = Object.freeze([
66
+ { kind: 'rate_limit', pattern: /rate.?limit|429|too many requests|quota exceeded|overloaded/i },
67
+ { kind: 'transient_outage', pattern: /50[234]\b|service unavailable|bad gateway|temporarily unavailable|ECONNRESET|ETIMEDOUT|ENOTFOUND|socket hang up/i },
68
+ { kind: 'timeout', pattern: /timed? ?out|deadline exceeded/i },
69
+ ]);
70
+
71
+ /** Infrastructure faults a human must repair — no amount of retrying helps. */
72
+ const INFRA = /not found|no such secret|invalid credentials|unauthori[sz]ed|401|403|permission denied|authentication|no auth type/i;
73
+
74
+ /** Damage a session cannot recover from. */
75
+ const CRITICAL = /corrupt|force.?push|history (?:rewritten|destroyed)|data loss/i;
76
+
77
+ /**
78
+ * Classify a failed turn.
79
+ *
80
+ * Order matters: critical damage is checked before anything else, and transient
81
+ * signals before infrastructure ones, because a rate-limited request often also
82
+ * mentions authentication in its error body.
83
+ */
84
+ export const classify = (result: SpawnResult): Classification => {
85
+ const text = `${result.stderr}\n${result.stdout}`.slice(0, 4000);
86
+
87
+ if (CRITICAL.test(text)) {
88
+ return {
89
+ fault_type: 'FAULT_INFRA',
90
+ path: 'PATH_C',
91
+ actor_penalised: false,
92
+ detail: 'irrecoverable damage reported',
93
+ };
94
+ }
95
+
96
+ if (result.timed_out) {
97
+ return {
98
+ fault_type: 'FAULT_EXTERNAL',
99
+ path: 'PATH_A',
100
+ actor_penalised: false,
101
+ detail: 'the agent exceeded its timeout',
102
+ };
103
+ }
104
+
105
+ for (const { kind, pattern } of SIGNALS) {
106
+ if (pattern.test(text)) {
107
+ return {
108
+ fault_type: 'FAULT_EXTERNAL',
109
+ path: 'PATH_A',
110
+ actor_penalised: false,
111
+ detail: `transient: ${kind}`,
112
+ };
113
+ }
114
+ }
115
+
116
+ if (INFRA.test(text)) {
117
+ return {
118
+ fault_type: 'FAULT_INFRA',
119
+ path: 'PATH_B',
120
+ actor_penalised: false,
121
+ detail: 'infrastructure or credentials require a human',
122
+ };
123
+ }
124
+
125
+ // Unrecognised. Deliberately not FAULT_ACTOR: blaming an actor for an
126
+ // unclassified failure is the exact injustice this module exists to prevent.
127
+ return {
128
+ fault_type: 'FAULT_AMBIGUOUS',
129
+ path: 'PATH_B',
130
+ actor_penalised: false,
131
+ detail: 'cannot determine — escalated to the Speaker for a ruling',
132
+ };
133
+ };
134
+
135
+ export const isAutoRecoverable = (c: Classification): boolean => c.path === 'PATH_A';
136
+
137
+ /* -------------------------------------------------------------------------- */
138
+ /* Retry policy */
139
+ /* -------------------------------------------------------------------------- */
140
+
141
+ export interface FaultHandling {
142
+ readonly auto_retry_max: number;
143
+ readonly auto_retry_delay_minutes: number;
144
+ readonly auto_recoverable: readonly RecoverableKind[];
145
+ }
146
+
147
+ export const DEFAULT_FAULT_HANDLING: FaultHandling = Object.freeze({
148
+ auto_retry_max: 3,
149
+ auto_retry_delay_minutes: 5,
150
+ auto_recoverable: AUTO_RECOVERABLE,
151
+ });
152
+
153
+ export interface RetryDecision {
154
+ readonly retry: boolean;
155
+ readonly attempt: number;
156
+ readonly of: number;
157
+ readonly delay_ms: number;
158
+ readonly reason: string;
159
+ }
160
+
161
+ /**
162
+ * Should this attempt be retried?
163
+ *
164
+ * `attempt` is 1-based and counts attempts already made.
165
+ */
166
+ export const shouldRetry = (
167
+ classification: Classification,
168
+ attempt: number,
169
+ policy: FaultHandling = DEFAULT_FAULT_HANDLING,
170
+ ): RetryDecision => {
171
+ const base = {
172
+ attempt,
173
+ of: policy.auto_retry_max,
174
+ delay_ms: policy.auto_retry_delay_minutes * 60_000,
175
+ };
176
+
177
+ if (!isAutoRecoverable(classification)) {
178
+ return { ...base, retry: false, delay_ms: 0, reason: `${classification.path} — not auto-recoverable` };
179
+ }
180
+ if (attempt >= policy.auto_retry_max) {
181
+ // Exhausted retries escalate to PATH_B rather than failing silently: a
182
+ // transient fault that never clears has become a human's problem.
183
+ return { ...base, retry: false, delay_ms: 0, reason: `retries exhausted (${attempt}/${policy.auto_retry_max}) — escalating to PATH_B` };
184
+ }
185
+ return { ...base, retry: true, reason: `${classification.detail} — retrying` };
186
+ };
187
+
188
+ /* -------------------------------------------------------------------------- */
189
+ /* Records */
190
+ /* -------------------------------------------------------------------------- */
191
+
192
+ export interface FaultRecordInput {
193
+ readonly at: string;
194
+ readonly actor: string;
195
+ readonly role: Role;
196
+ readonly action: string;
197
+ readonly classification: Classification;
198
+ readonly attempt?: number;
199
+ readonly of?: number;
200
+ }
201
+
202
+ /** Record a fault, with its attribution and resolution path. */
203
+ export const faultRecord = (input: FaultRecordInput): HansardEntry => ({
204
+ type:
205
+ input.classification.path === 'PATH_C'
206
+ ? RECORD_TYPES.SESSION_FAULT_CRITICAL
207
+ : RECORD_TYPES.SESSION_FAULT,
208
+ at: input.at,
209
+ actor: input.actor,
210
+ role: input.role,
211
+ fields: {
212
+ 'Action attempted': input.action,
213
+ 'Fault type': input.classification.fault_type,
214
+ 'Fault detail': input.classification.detail,
215
+ 'Actor penalised': input.classification.actor_penalised
216
+ ? 'Yes'
217
+ : 'No — not the actor\'s fault',
218
+ 'Resolution path': input.classification.path,
219
+ ...(input.attempt === undefined
220
+ ? {}
221
+ : { Attempt: `${input.attempt} of ${input.of ?? '?'}`, 'Auto retry': 'true' }),
222
+ },
223
+ });
224
+
225
+ /** Record that a retry succeeded, so the fault does not read as unresolved. */
226
+ export const autoResolvedRecord = (
227
+ at: string,
228
+ actor: string,
229
+ role: Role,
230
+ attempts: number,
231
+ ): HansardEntry => ({
232
+ type: RECORD_TYPES.FAULT_RESOLVED_AUTO,
233
+ at,
234
+ actor,
235
+ role,
236
+ fields: {
237
+ Resolution: `succeeded on attempt ${attempts}`,
238
+ 'Actor record': 'no penalty — the fault was transient',
239
+ 'Speaker intervention': 'none required',
240
+ },
241
+ });