Repository navigation
Expand file tree
/
Copy pathsettlement.py
More file actions
691 lines (629 loc) · 25.8 KB
/
Copy pathsettlement.py
File metadata and controls
691 lines (629 loc) · 25.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
"""Turn-driver adapter for the shared typed settlement receipt-chain driver."""
from __future__ import annotations
import inspect
from collections.abc import Callable, Mapping, Sequence
from dataclasses import dataclass
from typing import Any, Protocol
from ..effect_program import (
SettlementResult,
SettlementStepKind,
TurnProviderStepKind,
require_turn_provider_step_kind,
settlement_result_payload,
)
from ..effect_runtime import effect_runtime_result
from ..goals.source_session_turn_effects import (
SOURCE_TURN_EFFECT_HOLD_SCHEMA_VERSION,
)
from ..settlement_driver import decode_settlement_result
from .driver import read_turn_execution_context, selected_turn_todo
from .transaction import (
LoopXTurnResultKind,
require_loopx_turn_completion_outcome,
)
TurnEffect = Callable[..., Mapping[str, Any]]
TurnEffectResolver = Callable[[str], Mapping[str, Any]]
ResultEffect = Callable[..., Mapping[str, Any]]
TurnSettlementPrepare = Callable[[SettlementStepKind, str], None]
TurnSettlementAbort = Callable[[SettlementStepKind, str], None]
TurnSettlementCheckpoint = Callable[
[SettlementStepKind, Mapping[str, Any], tuple[str, ...]],
None,
]
TerminalCloseoutCheckpoint = Callable[[Mapping[str, Any]], None]
CompletionIntent = Callable[[Mapping[str, Any]], Mapping[str, Any]]
SourceJournalPersist = Callable[[Mapping[str, Any]], None]
class TurnSettlementEffectAdmission(Protocol):
def prepare(
self,
step_kind: TurnProviderStepKind,
effect_ref: str,
persist_journal: SourceJournalPersist,
) -> None: ...
def hold(
self,
step_kind: TurnProviderStepKind,
effect_ref: str,
persist_journal: SourceJournalPersist,
) -> None: ...
def release(
self,
step_kind: TurnProviderStepKind,
effect_ref: str,
persist_journal: SourceJournalPersist,
) -> None: ...
def allows_absent_reexecute(
self,
step_kind: TurnProviderStepKind,
effect_ref: str,
) -> bool: ...
@dataclass(frozen=True, slots=True)
class TurnSettlementState:
completed_phases: tuple[str, ...]
writeback: Mapping[str, Any] | None = None
quota_spend: Mapping[str, Any] | None = None
TURN_SETTLEMENT_TRANSACTION_SCHEMA_VERSION = "loopx_turn_settlement_transaction_v1"
TURN_SETTLEMENT_REDUCTION_SCHEMA_VERSION = "loopx_turn_settlement_reduction_v1"
def invoke_turn_effect(effect: TurnEffect, effect_ref: str) -> Mapping[str, Any]:
"""Invoke a provider with a stable ref while retaining zero-arg callbacks."""
try:
signature = inspect.signature(effect)
except (TypeError, ValueError):
return effect(effect_ref)
try:
signature.bind(effect_ref=effect_ref)
except TypeError:
pass
else:
return effect(effect_ref=effect_ref)
try:
signature.bind()
except TypeError:
signature.bind(effect_ref)
return effect(effect_ref)
return effect()
def invoke_result_effect(
effect: ResultEffect,
result: Mapping[str, Any],
effect_ref: str,
) -> Mapping[str, Any]:
"""Invoke a result provider with optional stable-ref compatibility."""
try:
signature = inspect.signature(effect)
except (TypeError, ValueError):
return effect(result, effect_ref)
try:
signature.bind(result, effect_ref=effect_ref)
except TypeError:
pass
else:
return effect(result, effect_ref=effect_ref)
try:
signature.bind(result)
except TypeError:
signature.bind(result, effect_ref)
return effect(result, effect_ref)
return effect(result)
def turn_effect_resolvers(
*,
writeback: TurnEffectResolver | None,
spend: TurnEffectResolver | None,
terminal_closeout: TurnEffectResolver | None,
) -> dict[SettlementStepKind, TurnEffectResolver]:
"""Collect configured provider readbacks under their typed steps."""
return {
step: resolver
for step, resolver in (
(SettlementStepKind.DURABLE_WRITEBACK, writeback),
(SettlementStepKind.QUOTA_SPEND, spend),
(SettlementStepKind.TERMINAL_CLOSEOUT, terminal_closeout),
)
if resolver is not None
}
@dataclass(frozen=True, slots=True)
class TurnSettlementJournalAdapter:
"""Mechanically persist prepared effects and committed provider payloads."""
journal: dict[str, Any]
effects: dict[str, bool]
persist: Callable[[], None]
compact_payload: Callable[[Mapping[str, Any]], Mapping[str, Any]]
source_effects: TurnSettlementEffectAdmission | None = None
persist_source: SourceJournalPersist | None = None
deferred_release_step: SettlementStepKind | None = None
@property
def effect_attempts(self) -> Mapping[str, Mapping[str, Any]]:
attempts = self.journal.get("effect_attempts")
return attempts if isinstance(attempts, Mapping) else {}
def prepare(self, step_kind: SettlementStepKind, effect_ref: str) -> None:
attempts = self.journal.setdefault("effect_attempts", {})
if not isinstance(attempts, dict):
raise RuntimeError("Turn journal effect_attempts shape mismatch")
attempts[step_kind.value] = {
"status": "prepared",
"effect_ref": effect_ref,
}
if self.source_effects is None:
self.persist()
return
self.source_effects.prepare(
require_turn_provider_step_kind(step_kind),
effect_ref,
self._require_source_persist(),
)
def abort(self, step_kind: SettlementStepKind, effect_ref: str) -> None:
self._forget(step_kind, effect_ref)
if self.source_effects is None:
self.persist()
return
self.source_effects.release(
require_turn_provider_step_kind(step_kind),
effect_ref,
self._require_source_persist(),
)
def checkpoint(
self,
step_kind: SettlementStepKind,
payload: Mapping[str, Any],
phases: tuple[str, ...],
) -> None:
effect_ref = self._effect_ref(step_kind, payload)
if step_kind is SettlementStepKind.DURABLE_WRITEBACK:
self.effects["state_written"] = True
self.journal["writeback"] = self._completion_payload(payload)
elif step_kind is SettlementStepKind.QUOTA_SPEND:
self.effects["quota_spent"] = True
self.journal["quota_spend"] = dict(self.compact_payload(payload))
self.journal["completed_phases"] = list(phases)
self._forget(step_kind, effect_ref)
if self.source_effects is None:
self.persist()
return
self._checkpoint_source_effect(step_kind, effect_ref)
def checkpoint_terminal(self, payload: Mapping[str, Any]) -> None:
step_kind = SettlementStepKind.TERMINAL_CLOSEOUT
effect_ref = self._effect_ref(step_kind, payload)
compact = self._completion_payload(payload)
self.journal["terminal_closeout"] = compact
writeback = self.journal.get("writeback")
self.journal["writeback"] = {
**(dict(writeback) if isinstance(writeback, Mapping) else {}),
"completion": compact.get("completion"),
}
self._forget(step_kind, effect_ref)
if self.source_effects is None:
self.persist()
return
self._checkpoint_source_effect(step_kind, effect_ref)
def allows_absent_reexecute(
self,
step_kind: SettlementStepKind,
effect_ref: str,
) -> bool:
if self.source_effects is None:
return True
return self.source_effects.allows_absent_reexecute(
require_turn_provider_step_kind(step_kind),
effect_ref,
)
def hold_tail(
self,
step_kind: SettlementStepKind,
effect_ref: str,
) -> None:
if self.source_effects is None:
raise RuntimeError("source Turn effect admission is unavailable")
self._set_tail_hold(step_kind, effect_ref)
self.source_effects.hold(
require_turn_provider_step_kind(step_kind),
effect_ref,
self._require_source_persist(),
)
def release_tail(
self,
step_kind: SettlementStepKind,
effect_ref: str,
) -> None:
expected = self._tail_hold(step_kind, effect_ref)
if self.journal.get("source_effect_hold") != expected:
raise RuntimeError("source Turn effect hold identity changed")
self.journal.pop("source_effect_hold")
if self.source_effects is None:
raise RuntimeError("source Turn effect admission is unavailable")
self.source_effects.release(
require_turn_provider_step_kind(step_kind),
effect_ref,
self._require_source_persist(),
)
def _checkpoint_source_effect(
self,
step_kind: SettlementStepKind,
effect_ref: str,
) -> None:
if self.source_effects is None:
raise RuntimeError("source Turn effect admission is unavailable")
persist = self._require_source_persist()
if step_kind is self.deferred_release_step:
self._set_tail_hold(step_kind, effect_ref)
self.source_effects.hold(
require_turn_provider_step_kind(step_kind),
effect_ref,
persist,
)
return
self.source_effects.release(
require_turn_provider_step_kind(step_kind),
effect_ref,
persist,
)
@staticmethod
def _tail_hold(
step_kind: SettlementStepKind,
effect_ref: str,
) -> dict[str, str]:
return {
"schema_version": SOURCE_TURN_EFFECT_HOLD_SCHEMA_VERSION,
"status": "held",
"step_kind": step_kind.value,
"effect_ref": effect_ref,
}
def _set_tail_hold(
self,
step_kind: SettlementStepKind,
effect_ref: str,
) -> None:
expected = self._tail_hold(step_kind, effect_ref)
existing = self.journal.get("source_effect_hold")
if existing is not None and existing != expected:
raise RuntimeError("source Turn effect hold identity changed")
self.journal["source_effect_hold"] = expected
def _completion_payload(self, payload: Mapping[str, Any]) -> dict[str, Any]:
return {
**dict(self.compact_payload(payload)),
**(
{"completion": dict(payload["completion"])}
if isinstance(payload.get("completion"), Mapping)
else {}
),
}
def _effect_ref(
self,
step_kind: SettlementStepKind,
payload: Mapping[str, Any],
) -> str:
payload_ref = str(payload.get("effect_ref") or "")
attempt = self.effect_attempts.get(step_kind.value)
attempt_ref = (
str(attempt.get("effect_ref") or "") if isinstance(attempt, Mapping) else ""
)
if attempt_ref:
if payload_ref and payload_ref != attempt_ref:
raise RuntimeError("Turn provider effect ref changed")
return attempt_ref
if payload_ref:
return payload_ref
raise RuntimeError("Turn journal prepared effect ref is missing")
def _require_source_persist(self) -> SourceJournalPersist:
if self.persist_source is None:
raise RuntimeError("source Turn effect persistence is unavailable")
return self.persist_source
def _forget(
self,
step_kind: SettlementStepKind,
effect_ref: str,
*,
strict: bool = True,
) -> None:
attempts = self.journal.get("effect_attempts")
if not isinstance(attempts, dict):
return
attempt = attempts.get(step_kind.value)
if (
isinstance(attempt, Mapping)
and effect_ref
and (attempt.get("effect_ref") != effect_ref)
):
if strict:
raise RuntimeError("Turn journal prepared effect ref changed")
return
attempts.pop(step_kind.value, None)
if not attempts:
self.journal.pop("effect_attempts", None)
def terminal_closeout_requirement(
*,
plan: Mapping[str, Any],
result: Mapping[str, Any],
journal: Mapping[str, Any],
completion_intent: CompletionIntent,
) -> tuple[bool, str | None]:
"""Classify final no-followup without mutating the Todo frontier."""
if result.get("result_kind") != LoopXTurnResultKind.VALIDATED_COMPLETION.value:
return False, None
stored_terminal = journal.get("terminal_closeout")
stored_writeback = journal.get("writeback")
terminal_payload = stored_terminal if isinstance(stored_terminal, Mapping) else {}
writeback_payload = (
stored_writeback if isinstance(stored_writeback, Mapping) else {}
)
observed_completion = terminal_payload.get("completion") or writeback_payload.get(
"completion"
)
try:
if not isinstance(observed_completion, Mapping):
observed_completion = completion_intent(result)
envelope = plan.get("turn_envelope")
if "work_contract" in plan:
# This is a new execution boundary, including journal recovery.
# Recheck the captured source after execution/intent; a pre-launch
# check must not authorize a later replacement of the adapter.
selected = read_turn_execution_context(
plan, envelope if isinstance(envelope, Mapping) else {},
)["selected_todo"]
else:
# Outstanding journals retain their exact historical decoder.
selected = selected_turn_todo(envelope) if isinstance(envelope, Mapping) else {}
outcome = require_loopx_turn_completion_outcome(
observed_completion,
expected_todo_id=str(selected.get("todo_id") or ""),
)
except (TypeError, ValueError) as exc:
return False, str(exc)
return outcome["continuation"] == "no_followup", None
def _read_prepared_effect(
resolvers: Mapping[SettlementStepKind, TurnEffectResolver],
step_kind: SettlementStepKind,
effect_ref: str,
) -> Mapping[str, Any]:
"""Transport provider evidence; TypeScript decides whether it permits IO."""
resolver = resolvers.get(step_kind)
if resolver is None:
return {"kind": "unknown", "reason": "provider readback is unavailable"}
try:
return dict(resolver(effect_ref))
except Exception as exc:
return {
"kind": "unknown",
"reason": f"provider readback raised {type(exc).__name__}",
}
def execute_turn_driver_settlement(
transaction_plan: Mapping[str, Any],
*,
transaction_phases: tuple[str, ...],
completed_phases: Sequence[str],
writeback_payload: Mapping[str, Any] | None,
quota_spend_payload: Mapping[str, Any] | None,
writeback: TurnEffect,
spend: TurnEffect,
checkpoint: TurnSettlementCheckpoint,
committed_effect_id: str | None = None,
terminal_closeout_required: bool = False,
terminal_closeout_payload: Mapping[str, Any] | None = None,
terminal_closeout: TurnEffect | None = None,
terminal_checkpoint: TerminalCloseoutCheckpoint | None = None,
prepare: TurnSettlementPrepare | None = None,
resume_prepare: TurnSettlementPrepare | None = None,
abort: TurnSettlementAbort | None = None,
allow_absent_reexecute: Callable[[SettlementStepKind, str], bool] | None = None,
effect_attempts: Mapping[str, Mapping[str, Any]] | None = None,
effect_resolvers: Mapping[SettlementStepKind, TurnEffectResolver] | None = None,
turn_result_kind: str | None = None,
) -> SettlementResult[TurnSettlementState]:
"""Run external effect providers and reduce one complete Turn settlement.
TypeScript first validates identity, replay, and the committed journal
prefix, then authorizes the still-Python providers in order. Python transports
provider returns and readbacks to TypeScript before executing its checkpoint
or retry decision. Only a persisted checkpoint advances the next provider. A replay
with no pending provider completes in the first reduction.
"""
phases = tuple(str(phase) for phase in completed_phases)
writeback_value = writeback_payload
spend_value = quota_spend_payload
terminal_value = terminal_closeout_payload
failed_attempt: tuple[SettlementStepKind, Mapping[str, Any]] | None = None
returned_attempt: Mapping[str, Any] | None = None
attempts = {
(step.value if isinstance(step, SettlementStepKind) else str(step)): dict(
attempt
)
for step, attempt in (effect_attempts or {}).items()
}
observations: dict[str, Mapping[str, Any]] = {}
resolvers = dict(effect_resolvers or {})
def reduce() -> Mapping[str, Any]:
payload = effect_runtime_result(
"turn.settlement.reduce",
{
"schema_version": TURN_SETTLEMENT_TRANSACTION_SCHEMA_VERSION,
"transaction_plan": dict(transaction_plan),
"transaction_phases": list(transaction_phases),
"completed_phases": list(phases),
"committed_effect_id": committed_effect_id,
"writeback_payload": (
dict(writeback_value)
if isinstance(writeback_value, Mapping)
else None
),
"quota_spend_payload": (
dict(spend_value) if isinstance(spend_value, Mapping) else None
),
"terminal_closeout_required": terminal_closeout_required,
"terminal_closeout_payload": (
dict(terminal_value)
if isinstance(terminal_value, Mapping)
else None
),
"failed_provider_attempt": (
{
"step_kind": failed_attempt[0].value,
"payload": dict(failed_attempt[1]),
}
if failed_attempt is not None
else None
),
"returned_provider_attempt": returned_attempt,
"effect_attempts": attempts,
"provider_observations": observations,
"turn_result_kind": turn_result_kind,
},
)
if (
not isinstance(payload, Mapping)
or payload.get("schema_version") != TURN_SETTLEMENT_REDUCTION_SCHEMA_VERSION
):
raise RuntimeError("TypeScript Turn settlement reduction shape mismatch")
return payload
reduction = reduce()
decision = str(reduction.get("decision") or "")
while decision == "execute":
provider_effects = reduction.get("provider_effects")
if not isinstance(provider_effects, list) or not provider_effects:
raise RuntimeError("TypeScript Turn settlement provider plan is empty")
providers = {
SettlementStepKind.DURABLE_WRITEBACK: writeback,
SettlementStepKind.QUOTA_SPEND: spend,
}
for raw_effect in provider_effects:
if not isinstance(raw_effect, Mapping):
raise RuntimeError("TypeScript Turn settlement provider shape mismatch")
step_kind = SettlementStepKind(str(raw_effect.get("step_kind") or ""))
action = str(raw_effect.get("action") or "")
effect_ref = str(raw_effect.get("effect_ref") or "")
if not effect_ref:
raise RuntimeError("TypeScript Turn settlement effect ref is empty")
if action == "resolve_prepared":
if resume_prepare is not None:
resume_prepare(step_kind, effect_ref)
observations[step_kind.value] = _read_prepared_effect(
resolvers, step_kind, effect_ref
)
continue
if action in {"prepare_and_execute", "execute_prepared"}:
if action == "prepare_and_execute":
if prepare is not None:
prepare(step_kind, effect_ref)
attempts[step_kind.value] = {
"status": "prepared",
"effect_ref": effect_ref,
}
elif (
allow_absent_reexecute is not None
and not allow_absent_reexecute(step_kind, effect_ref)
):
returned_attempt = {
"step_kind": step_kind.value,
"payload": {
"ok": False,
"appended": False,
"reason": "Goal retirement closed effect re-execution",
},
}
observations.pop(step_kind.value, None)
continue
provider = (
terminal_closeout
if step_kind is SettlementStepKind.TERMINAL_CLOSEOUT
else providers[step_kind]
)
if provider is None:
raise ValueError("terminal closeout requires an effect provider")
returned_attempt = {
"step_kind": step_kind.value,
"payload": dict(invoke_turn_effect(provider, effect_ref)),
}
observations.pop(step_kind.value, None)
continue
if action not in {"checkpoint", "abort_prepared"}:
raise RuntimeError("TypeScript Turn settlement action mismatch")
observed = raw_effect.get("payload")
if not isinstance(observed, Mapping):
raise RuntimeError("TypeScript Turn settlement payload shape mismatch")
if action == "abort_prepared":
if abort is not None:
abort(step_kind, effect_ref)
failed_attempt = (step_kind, observed)
elif step_kind is SettlementStepKind.TERMINAL_CLOSEOUT:
if terminal_checkpoint is None:
raise ValueError("terminal closeout requires a checkpoint")
terminal_checkpoint(observed)
terminal_value = observed
else:
raw_phases = raw_effect.get("completed_phases")
if not isinstance(raw_phases, list):
raise RuntimeError(
"TypeScript Turn settlement phases shape mismatch"
)
authorized_phases = tuple(str(phase) for phase in raw_phases)
checkpoint(step_kind, observed, authorized_phases)
phases = authorized_phases
if step_kind is SettlementStepKind.DURABLE_WRITEBACK:
writeback_value = observed
else:
spend_value = observed
attempts.pop(step_kind.value, None)
observations.pop(step_kind.value, None)
returned_attempt = None
reduction = reduce()
decision = str(reduction.get("decision") or "")
if decision not in {"complete", "failed"}:
raise RuntimeError("TypeScript Turn settlement did not reach an outcome")
def decode_state(value: Any) -> TurnSettlementState:
if not isinstance(value, Mapping):
raise RuntimeError("TypeScript Turn settlement state shape mismatch")
completed = value.get("completed_phases")
if not isinstance(completed, list):
raise RuntimeError("TypeScript Turn settlement phases shape mismatch")
raw_writeback = value.get("writeback")
raw_spend = value.get("quota_spend")
return TurnSettlementState(
completed_phases=tuple(str(phase) for phase in completed),
writeback=(
dict(raw_writeback) if isinstance(raw_writeback, Mapping) else None
),
quota_spend=(dict(raw_spend) if isinstance(raw_spend, Mapping) else None),
)
projection = reduction.get("settlement_result")
return decode_settlement_result(
reduction.get("result"),
value_decoder=decode_state,
projection_payload=(projection if isinstance(projection, Mapping) else None),
)
def turn_settlement_outcome(
result: SettlementResult[TurnSettlementState],
) -> Mapping[str, Any] | None:
"""Read the optional canonical Turn projection from the TS reduction."""
projection = settlement_result_payload(result)
outcome = projection.get("turn_outcome")
if not isinstance(outcome, Mapping):
return None
phases = outcome.get("completed_phases")
failed_phase = outcome.get("failed_phase")
if (
outcome.get("schema_version") != "loopx_turn_settlement_outcome_v0"
or not isinstance(outcome.get("result_kind"), str)
or not isinstance(phases, list)
or (failed_phase is not None and not isinstance(failed_phase, str))
):
raise RuntimeError("TypeScript Turn settlement outcome shape mismatch")
return dict(outcome)
def turn_settlement_failure_outcome(
result: SettlementResult[TurnSettlementState],
) -> tuple[LoopXTurnResultKind, tuple[str, ...], str]:
"""Decode a required TypeScript failure outcome for legacy projection."""
outcome = turn_settlement_outcome(result)
if outcome is None:
raise RuntimeError(
"TypeScript Turn settlement omitted its canonical failure outcome"
)
failed_phase = str(outcome.get("failed_phase") or "")
if not failed_phase:
raise RuntimeError("TypeScript Turn settlement failure omitted failed_phase")
try:
result_kind = LoopXTurnResultKind(str(outcome["result_kind"]))
except ValueError as exc:
raise RuntimeError(
"TypeScript Turn settlement failure has unsupported result_kind"
) from exc
return (
result_kind,
tuple(str(phase) for phase in outcome["completed_phases"]),
failed_phase,
)