Skip to content

Commit dedc271

Browse files
committed
Merge main into the scheduler follow-up receipt fence
Keep current main's projection-cache preview guard, host command rendering, and ledger window calculation while retaining this branch's tolerant scheduler readback and receipt fencing. Signed-off-by: ChengLong GU <88359731+gcl-coder@users.noreply.github.com>
2 parents ebbbb54 + 7abbe18 commit dedc271

25 files changed

Lines changed: 1136 additions & 312 deletions

‎loopx/cli_commands/quota_context.py‎

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
cached_goal_run_index_is_current,
1414
load_status_projection_cache,
1515
resolve_status_projection_cache_runtime_root,
16+
status_projection_cache_metadata,
1617
write_status_projection_cache,
1718
)
1819
from ..control_plane.scheduler.execution_context import (
@@ -362,21 +363,49 @@ def prepare_quota_command_context(
362363
available_capabilities=args.available_capabilities,
363364
agent_lane_id=args.agent_id,
364365
include_presentation_projections=False,
366+
# A scheduler ACK/failure return writes cadence, not settlement
367+
# facts. One damaged audit row must not fail the whole status read
368+
# before the tolerant native fence can answer it.
369+
tolerate_malformed_settlement_history=(
370+
command in QUOTA_SCHEDULER_FOLLOWUP_COMMANDS
371+
),
365372
)
366373
preview = command == "should-run" and getattr(args, "dry_run", False)
367374
if not preview and bool(getattr(args, "write_projection_cache", False)):
368-
cache_metadata = write_status_projection_cache(
369-
registry_path=registry_path,
370-
runtime_root=runtime_root,
371-
limit=status_limit,
372-
include_task_graph=False,
373-
goal_id=status_goal_id,
374-
payload=status_payload,
375-
max_age_seconds=projection_cache_ttl_seconds,
376-
available_capabilities=args.available_capabilities,
377-
agent_lane_id=args.agent_id,
378-
include_presentation_projections=False,
379-
)
375+
if command in QUOTA_SCHEDULER_FOLLOWUP_COMMANDS:
376+
# The follow-up collector tolerates malformed history so a
377+
# valid scheduler return can still be recorded. That partial
378+
# view must never become the cache consumed by strict status
379+
# readers under the same key.
380+
cache_metadata = status_projection_cache_metadata(
381+
registry_path=registry_path,
382+
runtime_root=runtime_root,
383+
limit=status_limit,
384+
include_task_graph=False,
385+
goal_id=status_goal_id,
386+
max_age_seconds=projection_cache_ttl_seconds,
387+
available_capabilities=args.available_capabilities,
388+
agent_lane_id=args.agent_id,
389+
include_presentation_projections=False,
390+
)
391+
cache_metadata.update({
392+
"hit": False,
393+
"written": False,
394+
"skipped_reason": "tolerant_scheduler_followup_view",
395+
})
396+
else:
397+
cache_metadata = write_status_projection_cache(
398+
registry_path=registry_path,
399+
runtime_root=runtime_root,
400+
limit=status_limit,
401+
include_task_graph=False,
402+
goal_id=status_goal_id,
403+
payload=status_payload,
404+
max_age_seconds=projection_cache_ttl_seconds,
405+
available_capabilities=args.available_capabilities,
406+
agent_lane_id=args.agent_id,
407+
include_presentation_projections=False,
408+
)
380409
elif isinstance(status_payload.get("projection_cache"), dict):
381410
cache_metadata = dict(status_payload["projection_cache"])
382411

‎loopx/cli_commands/quota_scheduler_followup.py‎

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ def _build_scheduler_followup_decision(
7676
repository_delivery_interaction_hook(repo_path=Path.cwd()),
7777
),
7878
goal_ref=goal_ref,
79+
# The compatibility ACK/failure route writes scheduler cadence, not
80+
# settlement facts: one damaged audit row must not turn a valid return
81+
# into a failure before the tolerant native fence runs. Exact
82+
# settlement branches keep the strict readback.
83+
tolerate_malformed_settlement_history=True,
7984
)
8085

8186

@@ -105,35 +110,6 @@ def build_scheduler_followup_payload(
105110
if turn_instance_id
106111
else None
107112
)
108-
if turn_instance_id and heartbeat_receipt is None:
109-
return {
110-
"ok": False,
111-
"schema_version": "quota_scheduler_followup_receipt_failure_v0",
112-
"mode": args.quota_command,
113-
"goal_id": args.goal_id,
114-
"agent_id": args.agent_id,
115-
"turn_instance_id": turn_instance_id,
116-
"decision": "skip",
117-
"should_run": False,
118-
"status": "heartbeat_receipt_missing",
119-
"state": "blocked_receipt",
120-
"error_code": "SCHEDULER_FOLLOWUP_HEARTBEAT_RECEIPT_MISSING",
121-
"reason": (
122-
"scheduler follow-up requires the originating heartbeat receipt; "
123-
"refusing to rebuild authority from the current live frontier"
124-
),
125-
"retry_guidance": (
126-
"retry with the exact Turn id from a committed quota should-run "
127-
"heartbeat receipt"
128-
),
129-
"write_performed": False,
130-
"appended": False,
131-
"registry_mutated": False,
132-
"scheduler_state_mutated": False,
133-
"quota_spend_performed": False,
134-
"delivery_outcome": "surface_only",
135-
}
136-
137113
observed_rrule = str(
138114
getattr(args, "app_automation_current_rrule", None)
139115
or getattr(args, "codex_app_current_rrule", None)
@@ -228,6 +204,8 @@ def build_scheduler_followup_payload(
228204
failed_rrule=args.failed_rrule,
229205
observed_host_rrule=observed_rrule,
230206
failure_kind=args.failure_kind,
207+
turn_instance_id=turn_instance_id,
208+
goal_ref=goal_ref,
231209
)
232210
return record_quota_scheduler_ack(
233211
status_payload,
@@ -248,4 +226,6 @@ def build_scheduler_followup_payload(
248226
scheduler_execution_context=scheduler_context,
249227
operator_inbox_urgency_projector=operator_inbox_urgency_projector,
250228
before_decision=before_decision,
229+
turn_instance_id=turn_instance_id,
230+
goal_ref=goal_ref,
251231
)

‎loopx/control_plane/quota/live_decision.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,7 @@ def build_live_quota_should_run_decision(
685685
workspace_path: Path | None = None,
686686
goal_ref: Mapping[str, object] | None = None,
687687
inline_goal_source: bool = False,
688+
tolerate_malformed_settlement_history: bool = False,
688689
) -> dict[str, Any]:
689690
"""Build one live CLI decision while keeping host observation injectable."""
690691
resolved_context = resolve_scheduler_execution_context(scheduler_execution_context)
@@ -742,6 +743,7 @@ def build_live_quota_should_run_decision(
742743
),
743744
registry_path=registry_path,
744745
goal_ref=goal_ref,
746+
tolerate_malformed_history=tolerate_malformed_settlement_history,
745747
)
746748
receipt_bound_monitor_phase = (
747749
settlement_readback.monitor_phase if settlement_readback else None

‎loopx/control_plane/quota/scheduler_ack.py‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
from collections.abc import Mapping
34
from typing import Any, cast
45

56
import json
@@ -184,14 +185,17 @@ def _run_native_followup(
184185
host_facts: dict[str, Any],
185186
use_current_hint: bool,
186187
reason_summary: str | None,
188+
turn_instance_id: str | None = None,
189+
goal_ref: Mapping[str, object] | None = None,
187190
) -> dict[str, Any]:
188191
"""Decision-free compatibility transport for in-process Python callers."""
189192

190193
request = {
191194
"schema_version": _FOLLOWUP_REQUEST_SCHEMA,
192195
"runtime_root": str(runtime_root),
193-
"turn_instance_id": None,
194-
"require_heartbeat_receipt": False,
196+
"goal_ref": dict(goal_ref) if goal_ref is not None else None,
197+
"turn_instance_id": turn_instance_id,
198+
"require_heartbeat_receipt": bool(turn_instance_id),
195199
"before": before,
196200
"host_facts": host_facts,
197201
"use_current_hint": use_current_hint,
@@ -298,6 +302,8 @@ def record_quota_scheduler_ack_for_decision(
298302
generated_at: str | None = None,
299303
use_current_hint: bool = False,
300304
host_match_observed: bool = False,
305+
turn_instance_id: str | None = None,
306+
goal_ref: Mapping[str, object] | None = None,
301307
) -> dict[str, Any]:
302308
state_key = _followup_state_key(before, surface=surface, state_key=state_key)
303309
safe_agent_id = normalize_todo_claimed_by(agent_id)
@@ -367,6 +373,8 @@ def record_quota_scheduler_ack_for_decision(
367373
host_facts=facts,
368374
use_current_hint=use_current_hint,
369375
reason_summary=reason_summary,
376+
turn_instance_id=turn_instance_id,
377+
goal_ref=goal_ref,
370378
)
371379
except ValueError as exc:
372380
return _failure_payload(
@@ -396,6 +404,8 @@ def record_quota_scheduler_failure_for_decision(
396404
observed_host_rrule: str | None = None,
397405
failure_kind: str = "host_tool_failure",
398406
generated_at: str | None = None,
407+
turn_instance_id: str | None = None,
408+
goal_ref: Mapping[str, object] | None = None,
399409
) -> dict[str, Any]:
400410
state_key = _followup_state_key(before, surface=surface, state_key=state_key)
401411
safe_agent_id = normalize_todo_claimed_by(agent_id)
@@ -443,6 +453,8 @@ def record_quota_scheduler_failure_for_decision(
443453
host_facts=facts,
444454
use_current_hint=False,
445455
reason_summary=None,
456+
turn_instance_id=turn_instance_id,
457+
goal_ref=goal_ref,
446458
)
447459
except ValueError as exc:
448460
return _failure_payload(

‎loopx/control_plane/quota/settlement.py‎

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -233,22 +233,13 @@ def attach_settlement_progress(
233233
}
234234
# PowerShell quoting applies to every argument, even with an unquoted launcher.
235235
# Keep the argv for the composed CLI so it does not have to parse shell text.
236-
if os.name == "nt" or route[0].lower().endswith(".ps1"):
236+
if os.name == "nt":
237237
argv = shlex.split(command)
238238
owed.update(command=render_owed_host_command(argv), command_argv=argv)
239239
payload["settlement_owed"] = owed
240240

241241

242242
def render_settlement_progress_markdown(payload: dict[str, Any]) -> list[str]:
243-
recovery = payload.get("settlement_recovery")
244-
if isinstance(recovery, dict):
245-
identity = recovery["settlement_identity"]
246-
lines = [f"- Pending settlement: `{identity['turn_instance_id']}`", recovery["instruction"]]
247-
for command in recovery["next_cli_actions"]:
248-
lines.extend(["", "```sh", command, "```"])
249-
return lines
250-
if payload.get("settlement_recovery_error"):
251-
return [f"- Settlement recovery could not be read: {payload['settlement_recovery_error']}"]
252243
progress = payload.get("settlement_progress")
253244
if not isinstance(progress, dict):
254245
return []
@@ -466,14 +457,14 @@ def read_heartbeat_settlement(
466457
infer_turn_instance_id: bool = False,
467458
allow_unbound_binding: bool = False,
468459
resolve_original_binding: bool = False,
469-
pending_closeout: bool = False,
470460
refresh_retry: dict[str, Any] | None = None,
471461
registry_path: Path | None = None,
472462
goal_ref: Mapping[str, Any] | None = None,
473463
source_admission: Mapping[str, Any] | None = None,
474464
borrow_source_admission: bool = False,
475465
heartbeat_reentry_guard: dict[str, Any] | None = None,
476466
include_record_bodies: bool = True,
467+
tolerate_malformed_history: bool = False,
477468
) -> QuotaSettlementReadback | None:
478469
"""Read one complete heartbeat settlement through the TS domain owner."""
479470

@@ -492,8 +483,12 @@ def read_heartbeat_settlement(
492483
if include_record_bodies is not True
493484
else {}
494485
),
486+
**(
487+
{"tolerate_malformed_history": True}
488+
if tolerate_malformed_history
489+
else {}
490+
),
495491
**({"resolve_original_binding": True} if resolve_original_binding else {}),
496-
**({"pending_closeout": True} if pending_closeout else {}),
497492
**({"refresh_retry": refresh_retry} if refresh_retry is not None else {}),
498493
**({"borrow_source_admission": True} if borrow_source_admission else {}),
499494
**(

0 commit comments

Comments
 (0)