Skip to content

Official Codebase of 3DCodeVerse

Python

zone layout: obeys the run clock — preflight per call, wait clipped

gaoypeng17903ca00a96
diff --git a/harness/codeverse/tracks/generation.py b/harness/codeverse/tracks/generation.py
index cfff2c1c..f77488ec 100644
--- a/harness/codeverse/tracks/generation.py
+++ b/harness/codeverse/tracks/generation.py
@@ -508,7 +508,8 @@ def _charge(budget: Any | None, usage: Usage, *, task: GenerationTask, enforce:
budget.charge(usage, stage=task_stage(task), enforce=enforce)
-def _deadline_preflight(budget: Any | None, wait_s: float, *, soft: bool = False) -> float:
+def _deadline_preflight(budget: Any | None, wait_s: float, *, soft: bool = False,
+ floor_s: float = 120.0) -> float:
"""Gate a session on the run clock: refuse to start past the HARD ceiling (a
timed-out session that produced nothing bills nothing, so the accounting-driven
check alone never fires) and clip the wait to the remaining wall clock (a flat
@@ -518,7 +519,7 @@ def _deadline_preflight(budget: Any | None, wait_s: float, *, soft: bool = False
if hasattr(budget, "check"):
budget.check()
if hasattr(budget, "timeout_s"):
- wait_s = budget.timeout_s(wait_s, floor_s=120.0, soft=soft)
+ wait_s = budget.timeout_s(wait_s, floor_s=floor_s, soft=soft)
return wait_s
diff --git a/harness/codeverse/tracks/zone_layout.py b/harness/codeverse/tracks/zone_layout.py
index 1a00ea02..0415f852 100644
--- a/harness/codeverse/tracks/zone_layout.py
+++ b/harness/codeverse/tracks/zone_layout.py
@@ -23,8 +23,10 @@ from codeverse.contracts.chat import ChatMessage, ChatRequest
from codeverse.contracts.plan import ScenePlan, ZoneLayout, ZonePlan
from codeverse.conventions import to_snake
from codeverse.models.schema_utils import parse_json_lenient
+from codeverse.orchestrator import BudgetExceeded
from codeverse.proc import fan_out
from codeverse.prompts import render
+from codeverse.tracks.generation import _deadline_preflight
log = logging.getLogger(__name__)
@@ -142,11 +144,21 @@ def _one_layout(zone: ZonePlan, plan: ScenePlan, model: Any, budget: Any, events
user = f"Lay out zone {zone.name}. Description (binding): {zone.description}\nContents to place: {', '.join(zone.contents) or '(none)'}"
complaint = ""
for attempt in (0, 1):
+ # the run clock outranks the layout (review-3 S3): past the hard ceiling the
+ # documented degraded mode is "no layout for this zone", and a call that does
+ # go out gets the wall clock actually left, never a flat 300 s — the re-ask
+ # preflights again, so it cannot buy a second window past the ceiling
+ try:
+ max_wait_s = _deadline_preflight(budget, LAYOUT_WAIT_S, soft=True, floor_s=20.0)
+ except BudgetExceeded as e:
+ if events is not None:
+ events.emit("layout.skipped_budget", zone=zone.name, error=str(e)[:200])
+ return None
ask = user if not complaint else f"{user}\n\nYour previous layout was rejected: {complaint}. Fix exactly these problems."
resp = model.generate(ChatRequest(messages=[ChatMessage.user(ask)], system=system,
response_schema=ZoneLayout.model_json_schema(), temperature=0.3,
thinking="low", max_output_tokens=LAYOUT_MAX_TOKENS,
- max_wait_s=LAYOUT_WAIT_S, label="zone-layout"))
+ max_wait_s=max_wait_s, label="zone-layout"))
if budget is not None:
budget.add(resp.usage, stage="plan")
raw = resp.parsed if resp.parsed is not None else parse_json_lenient(resp.text or "{}")
diff --git a/harness/tests/orchestrator_tracks/test_zone_layout.py b/harness/tests/orchestrator_tracks/test_zone_layout.py
index 66da13f6..1bc347b3 100644
--- a/harness/tests/orchestrator_tracks/test_zone_layout.py
+++ b/harness/tests/orchestrator_tracks/test_zone_layout.py
@@ -120,3 +120,43 @@ def test_layout_zones_drops_a_twice_rejected_zone_instead_of_dying():
good_market = {"zone": "Market", "placements": [{"asset": "Stall", "count": 4, "cluster": (10.0, 0.0), "spread_m": 5.0}]}
out = layout_zones(plan, _Model([bad, bad, good_market]), max_workers=1)
assert set(out) == {"Market"}
+
+
+def test_a_run_past_its_ceiling_buys_no_layout_calls():
+ """Review-3 S3 (V5-claim2): past the hard ceiling, layout_zones makes ZERO model
+ calls and returns no layouts (the documented degraded mode) instead of buying up
+ to 2 x 300 s per zone."""
+ import time
+
+ from codeverse.contracts.common import Budget
+ from codeverse.orchestrator import BudgetGuard
+
+ model = _Model([]) # any call would pop an empty queue and explode the test
+ guard = BudgetGuard(Budget(max_minutes=30), start_time=time.time() - 45 * 60)
+ out = layout_zones(_plan(), model, budget=guard, max_workers=1)
+ assert out == {} and model.calls == 0
+
+
+def test_layout_waits_are_clipped_to_the_remaining_run_clock():
+ """A call that does go out asks for min(LAYOUT_WAIT_S, wall clock left), floored."""
+ import time
+
+ from codeverse.contracts.common import Budget
+ from codeverse.orchestrator import BudgetGuard
+
+ class _Recorder(_Model):
+ def __init__(self, answers):
+ super().__init__(answers)
+ self.waits = []
+
+ def generate(self, req):
+ self.waits.append(req.max_wait_s)
+ return super().generate(req)
+
+ good_quay = _layout().model_dump(mode="json")
+ good_market = {"zone": "Market", "placements": [{"asset": "Stall", "count": 4, "cluster": (10.0, 0.0), "spread_m": 5.0}]}
+ model = _Recorder([good_quay, good_market])
+ guard = BudgetGuard(Budget(max_minutes=30), start_time=time.time() - 28 * 60) # ~2 min left
+ out = layout_zones(_plan(), model, budget=guard, max_workers=1)
+ assert set(out) == {"Quay", "Market"}
+ assert model.waits and all(20 <= w <= 125 for w in model.waits), model.waits