Skip to content

Commit d9987e3

Browse files
committed
refactor(quota): isolate deferred receipt projection
Signed-off-by: huangruiteng <14976749+huangruiteng@users.noreply.github.com>
1 parent 2d6485f commit d9987e3

3 files changed

Lines changed: 76 additions & 33 deletions

File tree

‎loopx/control_plane/quota/settlement_precedence.py‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
"the receipt-bound work binding and required settlement receipts "
1010
"are complete for this heartbeat turn; defer successor selection to a new turn"
1111
)
12+
RECEIPT_BOUND_DEFERRED_REASON = (
13+
"the Todo bound to this heartbeat receipt is deferred; do not "
14+
"select or spend an independent successor in the same Turn"
15+
)
1216

1317
_ACTION_PROJECTION_KEYS = (
1418
"agent_command",
@@ -89,3 +93,26 @@ def settled_replay_fields() -> dict[str, Any]:
8993
"spend_policy": "no quota spend for an already-settled heartbeat turn",
9094
},
9195
}
96+
97+
98+
def deferred_receipt_bound_skip_fields(
99+
quota: dict[str, Any],
100+
heartbeat_recommendation: dict[str, Any],
101+
) -> tuple[str, str, dict[str, Any], dict[str, Any]]:
102+
"""Project a deferred receipt without borrowing a successor's authority."""
103+
104+
effective_action = EffectiveAction.QUOTA_SKIP.value
105+
reason = RECEIPT_BOUND_DEFERRED_REASON
106+
return (
107+
effective_action,
108+
reason,
109+
{**quota, "safe_bypass_allowed": False},
110+
{
111+
**heartbeat_recommendation,
112+
"recommended_mode": effective_action,
113+
"notify": "DONT_NOTIFY",
114+
"reason": reason,
115+
"spend_policy": "no quota spend for a deferred receipt-bound Todo",
116+
"stop_if_unchanged": True,
117+
},
118+
)

‎loopx/control_plane/quota/should_run_packet.py‎

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@
129129
work_lane_contract_is_receipt_bound_monitor_settled,
130130
)
131131
from .settlement_precedence import (
132+
deferred_receipt_bound_skip_fields,
132133
settled_replay_fields,
133134
HEARTBEAT_SETTLED_REPLAY_REASON,
134135
clear_quota_action_projections,
@@ -889,31 +890,32 @@ def _resolve_quota_should_run_route(
889890
"reason": reason,
890891
"spend_policy": "no quota spend for an already-settled heartbeat turn",
891892
}
892-
receipt_bound_deferred_wait = bool(
893+
if (
893894
prepared.receipt_bound_todo_id
894895
and isinstance(prepared.work_lane_contract, dict)
895896
and prepared.work_lane_contract.get("obligation")
896897
== WORK_LANE_RECEIPT_BOUND_DEFERRED_OBLIGATION
897-
)
898-
if receipt_bound_deferred_wait:
899-
normal_delivery_allowed = recovery_allowed = self_repair_allowed = False
900-
capability_repair_allowed = workspace_repair_allowed = False
901-
replan_decision_allowed = receipt_bound_replan_decision = False
902-
should_run = False
903-
effective_action = EffectiveAction.QUOTA_SKIP.value
904-
reason = (
905-
"the Todo bound to this heartbeat receipt is deferred; do not "
906-
"select or spend an independent successor in the same Turn"
898+
):
899+
# Every action path is closed for this immutable, deferred receipt.
900+
(
901+
normal_delivery_allowed,
902+
recovery_allowed,
903+
self_repair_allowed,
904+
capability_repair_allowed,
905+
workspace_repair_allowed,
906+
replan_decision_allowed,
907+
receipt_bound_replan_decision,
908+
should_run,
909+
) = (False,) * 8
910+
(
911+
effective_action,
912+
reason,
913+
quota,
914+
heartbeat_recommendation,
915+
) = deferred_receipt_bound_skip_fields(
916+
quota,
917+
heartbeat_recommendation,
907918
)
908-
quota = {**quota, "safe_bypass_allowed": False}
909-
heartbeat_recommendation = {
910-
**heartbeat_recommendation,
911-
"recommended_mode": effective_action,
912-
"notify": "DONT_NOTIFY",
913-
"reason": reason,
914-
"spend_policy": "no quota spend for a deferred receipt-bound Todo",
915-
"stop_if_unchanged": True,
916-
}
917919
monitor_quiet_skip = (
918920
not replan_decision_allowed
919921
and normal_delivery_allowed

‎loopx/control_plane/quota/should_run_prepare.py‎

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,20 @@ def _build_agent_work_lane(
452452
return monitor_only, work_lane, task_orchestration
453453

454454

455+
def _deferred_receipt_bound_work_lane(
456+
*, todo_id: str, source_items: list[dict[str, Any]],
457+
) -> dict[str, Any] | None:
458+
"""Keep a deferred Todo's old receipt visible without selecting new work."""
459+
460+
if any(
461+
normalize_todo_id(source_item.get("todo_id")) == todo_id
462+
and normalize_todo_status(source_item.get("status")) == TODO_STATUS_DEFERRED
463+
for source_item in source_items
464+
):
465+
return receipt_bound_deferred_work_lane(todo_id=todo_id)
466+
return None
467+
468+
455469
def _prepare_quota_should_run_item(
456470
status_payload: dict[str, Any],
457471
*,
@@ -738,24 +752,24 @@ def _prepare_quota_should_run_item(
738752
and candidate.get("selection_binding") == "heartbeat_receipt"
739753
):
740754
receipt_bound_agent_next_action = candidate
741-
preserved_work_lane = preserve_heartbeat_receipt_bound_work_lane(
742-
work_lane_contract,
743-
selected_todo=candidate,
755+
work_lane_contract = (
756+
preserve_heartbeat_receipt_bound_work_lane(
757+
work_lane_contract,
758+
selected_todo=candidate,
759+
)
760+
or work_lane_contract
744761
)
745-
if isinstance(preserved_work_lane, dict):
746-
work_lane_contract = preserved_work_lane
747-
elif any(
748-
normalize_todo_id(source_item.get("todo_id")) == receipt_bound_todo_id
749-
and normalize_todo_status(source_item.get("status"))
750-
== TODO_STATUS_DEFERRED
751-
for source_item in agent_todo_planning_source_items
752-
):
762+
else:
753763
# The old Turn still owns its committed settlement identity, but a
754764
# deferred Todo is not an executable candidate. A successor may be
755765
# selected only by a fresh Turn; do not leak it through work-lane
756766
# fallback on this replay.
757-
work_lane_contract = receipt_bound_deferred_work_lane(
758-
todo_id=receipt_bound_todo_id,
767+
work_lane_contract = (
768+
_deferred_receipt_bound_work_lane(
769+
todo_id=receipt_bound_todo_id,
770+
source_items=agent_todo_planning_source_items,
771+
)
772+
or work_lane_contract
759773
)
760774
if inbox_priority_due:
761775
task_orchestration_contract = capability_gate = capability_monitor_contract = None

0 commit comments

Comments
 (0)