@@ -142,6 +142,44 @@ def resolve_due_monitor_candidate(
142142 }
143143
144144
145+ def _monitor_poll_receipt_path (runtime_root : Path , goal_id : str , effect_id : str ) -> Path :
146+ return (runtime_root / "goals" / goal_id / "runs" / ".transactions" /
147+ "quota-monitor-poll" / f"{ hashlib .sha256 (effect_id .encode ()).hexdigest ()[:24 ]} .json" )
148+
149+
150+ def resolve_monitor_poll_target (
151+ * , registry_path : Path | None , runtime_root : Path | None , goal_id : str ,
152+ target_key : str | None , before : dict [str , Any ], turn_instance_id : str | None ,
153+ execute : bool , goal_ref : Mapping [str , Any ] | None ,
154+ ) -> tuple [str | None , str | None ]:
155+ due = resolve_due_monitor_candidate (registry_path = registry_path , runtime_root = runtime_root ,
156+ goal_id = goal_id , todo_id = None , target_key = target_key )
157+ if due :
158+ return normalize_todo_id (due ["todo_id" ]), None
159+ if not execute or runtime_root is None or not turn_instance_id or not target_key or registry_path is None :
160+ return None , None
161+ try :
162+ item = resolve_monitor_todo_item (registry_path = registry_path , runtime_root = runtime_root ,
163+ goal_id = goal_id , target_key = target_key )
164+ except ValueError :
165+ return None , None
166+ todo_id = normalize_todo_id (item .get ("todo_id" ))
167+ effect_id = _monitor_poll_effect_id (runtime_root = runtime_root , goal_id = goal_id ,
168+ agent_id = quota_decision_agent_id (before ), turn_instance_id = normalize_turn_instance_id (turn_instance_id ),
169+ todo_id = todo_id , target_key = target_key , goal_ref = goal_ref )
170+ expected_effect = f"quota-monitor-poll:{ goal_id } :{ quota_decision_agent_id (before )} :{ normalize_turn_instance_id (turn_instance_id )} :todo:{ todo_id } "
171+ if effect_id != expected_effect :
172+ # Historical target/turn-only effects can have a null Todo on the wire.
173+ # The provider's resolved Todo cannot rewrite their frozen fingerprint.
174+ return None , None
175+ # Existence is only a transport route to the original operation. TypeScript
176+ # validates scope, fingerprint and frozen admission before replay/recovery.
177+ # Without that operation, retain current due admission and the original wire.
178+ if _monitor_poll_receipt_path (runtime_root , goal_id , effect_id ).exists ():
179+ return todo_id , effect_id
180+ return None , None
181+
182+
145183def _decision_packet (
146184 before : dict [str , Any ],
147185 * ,
@@ -739,6 +777,7 @@ def record_quota_monitor_poll_for_decision(
739777 _index_lock_held : bool = False ,
740778 status_reloader : Callable [[], dict [str , Any ]] | None = None ,
741779 goal_ref : Mapping [str , Any ] | None = None ,
780+ recovery_effect_id : str | None = None ,
742781) -> dict [str , Any ]:
743782 del render_markdown
744783 normalized_turn_id = normalize_turn_instance_id (turn_instance_id )
@@ -855,6 +894,14 @@ def failure(
855894 def transact (
856895 source_admission : Mapping [str , Any ] | None ,
857896 ) -> tuple [dict [str , Any ], dict [str , Any ]]:
897+ # Recheck the transport precondition inside the same quota admission as
898+ # the native receipt read. A legal no-effect abort may have removed it
899+ # since target selection; that must never turn recovery into a new poll.
900+ if recovery_effect_id is not None and (
901+ effect_id != recovery_effect_id or
902+ not _monitor_poll_receipt_path (runtime_root , goal_id , effect_id ).exists ()
903+ ):
904+ raise ValueError ("original Monitor poll transaction is unavailable; reconcile its original operation before retrying" )
858905 common = {
859906 "effect_id" : effect_id ,
860907 "runtime_root" : runtime_root ,
@@ -876,8 +923,7 @@ def transact(
876923
877924 # Validate explicit claim routing before quota reserves a pending effect.
878925 # Exact retries keep their frozen admission if registry membership changed.
879- receipt_path = (runtime_root / "goals" / goal_id / "runs" / ".transactions" /
880- "quota-monitor-poll" / f"{ hashlib .sha256 (effect_id .encode ()).hexdigest ()[:24 ]} .json" )
926+ receipt_path = _monitor_poll_receipt_path (runtime_root , goal_id , effect_id )
881927 if next_claimed_by and registry_path is not None and not receipt_path .exists ():
882928 plan_todo_authoring_scope (
883929 command = "create" , role = "agent" , goal_id = goal_id ,
0 commit comments