|
28 | 28 | SettlementResult, |
29 | 29 | SettlementStep, |
30 | 30 | SettlementStepKind, |
| 31 | + ReceiptBoundMonitorPhase, |
31 | 32 | build_codex_app_settlement_plan, |
32 | 33 | build_turn_scoped_cli_settlement_plan, |
33 | 34 | settlement_binding_args, |
| 35 | + receipt_bound_monitor_phase, |
34 | 36 | settlement_result_payload, |
35 | 37 | settlement_step_command, |
36 | 38 | ) |
37 | 39 | from .heartbeat_receipt import find_heartbeat_receipt |
| 40 | +from .monitor_poll import find_quota_monitor_poll_turn |
38 | 41 | from .settlement_workspace_causality import ( |
39 | 42 | delivery_workspace_causality_from_event_details, |
40 | 43 | ) |
|
59 | 62 | "find_settlement_step_event", |
60 | 63 | "find_settlement_writeback", |
61 | 64 | "infer_persisted_heartbeat_settlement_identity", |
| 65 | + "receipt_bound_monitor_settlement_phase", |
62 | 66 | "require_settlement_spend", |
63 | 67 | "require_settlement_terminal_closeout", |
64 | 68 | "require_settlement_writeback", |
|
70 | 74 | ] |
71 | 75 |
|
72 | 76 |
|
| 77 | +def receipt_bound_monitor_settlement_phase( |
| 78 | + runtime_root: Path, |
| 79 | + *, |
| 80 | + goal_id: str, |
| 81 | + agent_id: str | None, |
| 82 | + todo_id: str | None, |
| 83 | + turn_instance_id: str | None, |
| 84 | +) -> ReceiptBoundMonitorPhase | None: |
| 85 | + """Resolve the typed phase for one receipt-bound monitor turn. |
| 86 | +
|
| 87 | + A missing matching poll remains explicitly ``poll_due`` even when mutable |
| 88 | + Todo scheduling metadata has moved into the future. An unchanged poll is |
| 89 | + terminal by itself. A material poll is terminal only after the exact |
| 90 | + heartbeat identity has both durable writeback and quota-spend receipts. |
| 91 | + ``None`` is reserved for an invalid receipt identity or runtime failure. |
| 92 | + """ |
| 93 | + |
| 94 | + normalized_agent_id = normalize_todo_claimed_by(agent_id) |
| 95 | + normalized_todo_id = normalize_todo_id(todo_id) |
| 96 | + try: |
| 97 | + normalized_turn_id = normalize_turn_instance_id(turn_instance_id) |
| 98 | + except ValueError: |
| 99 | + return None |
| 100 | + if not normalized_agent_id or not normalized_todo_id or not normalized_turn_id: |
| 101 | + return None |
| 102 | + poll = find_quota_monitor_poll_turn( |
| 103 | + runtime_root, |
| 104 | + goal_id=goal_id, |
| 105 | + agent_id=normalized_agent_id, |
| 106 | + turn_instance_id=normalized_turn_id, |
| 107 | + ) |
| 108 | + if not isinstance(poll, Mapping): |
| 109 | + return receipt_bound_monitor_phase( |
| 110 | + poll_present=False, |
| 111 | + material_change=False, |
| 112 | + durable_writeback_present=False, |
| 113 | + quota_spend_present=False, |
| 114 | + ) |
| 115 | + if normalize_todo_id(poll.get("todo_id")) != normalized_todo_id: |
| 116 | + return receipt_bound_monitor_phase( |
| 117 | + poll_present=False, |
| 118 | + material_change=False, |
| 119 | + durable_writeback_present=False, |
| 120 | + quota_spend_present=False, |
| 121 | + ) |
| 122 | + material_change = poll.get("material_change") is True |
| 123 | + if not material_change: |
| 124 | + return receipt_bound_monitor_phase( |
| 125 | + poll_present=True, |
| 126 | + material_change=False, |
| 127 | + durable_writeback_present=False, |
| 128 | + quota_spend_present=False, |
| 129 | + ) |
| 130 | + identity_result = resolve_heartbeat_settlement_identity( |
| 131 | + runtime_root, |
| 132 | + goal_id=goal_id, |
| 133 | + agent_id=normalized_agent_id, |
| 134 | + todo_id=normalized_todo_id, |
| 135 | + turn_instance_id=normalized_turn_id, |
| 136 | + ) |
| 137 | + identity = identity_result.value |
| 138 | + durable_writeback_present = bool( |
| 139 | + identity is not None |
| 140 | + and require_settlement_writeback(runtime_root, identity).value is not None |
| 141 | + ) |
| 142 | + quota_spend_present = bool( |
| 143 | + identity is not None |
| 144 | + and require_settlement_spend(runtime_root, identity).value is not None |
| 145 | + ) |
| 146 | + return receipt_bound_monitor_phase( |
| 147 | + poll_present=True, |
| 148 | + material_change=True, |
| 149 | + durable_writeback_present=durable_writeback_present, |
| 150 | + quota_spend_present=quota_spend_present, |
| 151 | + ) |
| 152 | + |
| 153 | + |
73 | 154 | def resolve_settlement_delivery_workspace_causality( |
74 | 155 | runtime_root: Path, |
75 | 156 | identity: SettlementIdentity, |
|
0 commit comments