Skip to content

Official Codebase of 3DCodeVerse

Python

generation: single-shot obeys the run clock, one preflight owner for both paths

gaoypengc60f0a9c57a2
diff --git a/harness/codeverse/tracks/generation.py b/harness/codeverse/tracks/generation.py
index 0ceea7dd..ff9f5a85 100644
--- a/harness/codeverse/tracks/generation.py
+++ b/harness/codeverse/tracks/generation.py
@@ -373,6 +373,8 @@ def generate_files(
allowed_roots: tuple[str, ...] = ALLOWED_ROOTS,
) -> GenerationResult:
"""Single-shot API codegen: ChatRequest → envelope → files on disk (no commit)."""
+ from codeverse.models.retry import RETRY_DEADLINE_S
+
system = task.system + ("\n\n" if task.system else "") + single_shot_format_text()
req = ChatRequest(
messages=[ChatMessage.user(task.prompt, images=task.images or None)],
@@ -380,6 +382,9 @@ def generate_files(
temperature=task.temperature,
thinking=task.thinking, # type: ignore[arg-type]
max_output_tokens=task.max_output_tokens,
+ # the RUN's remaining wall clock, not the models' 1800 s default — and past
+ # the hard ceiling this refuses to buy the call at all (agent-path symmetry)
+ max_wait_s=_deadline_preflight(budget, RETRY_DEADLINE_S),
# the round tag is the cost ledger's only way to attribute a single-shot call:
# it has no agent session to inherit an ambient round from (codeverse.cost.context)
label=f"{task.label}:r{task.round:02d}",
@@ -408,7 +413,10 @@ def generate_files(
retry=retry,
)
if retry:
- req = req.model_copy(update={"max_output_tokens": grown})
+ # the retry is a second full-price call: preflight the clock again and
+ # hand it only what is left of the run (BudgetExceeded past the ceiling)
+ req = req.model_copy(update={"max_output_tokens": grown,
+ "max_wait_s": _deadline_preflight(budget, RETRY_DEADLINE_S)})
resp = model.generate(req)
usage = usage + resp.usage
_charge(budget, resp.usage, task=task,
@@ -500,6 +508,22 @@ 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:
+ """Gate a model/agent session on the run clock: refuse to start past the HARD
+ ceiling (``budget.check()`` raises — a timed-out session that produced nothing
+ bills nothing, so the accounting-driven check alone never fires), and clip the
+ session's wait to the remaining wall clock (a flat 30-minute session once ran
+ 9 minutes PAST a 30-minute ceiling, measured 2026-08-27). ``soft=True`` keeps
+ a caller-chosen window as its stage clipped it (scene.py's shares)."""
+ if budget is None:
+ return wait_s
+ if hasattr(budget, "check"):
+ budget.check()
+ if hasattr(budget, "timeout_s"):
+ wait_s = budget.timeout_s(wait_s, floor_s=120.0, soft=soft)
+ return wait_s
+
+
_TRUNCATED_FINISH = {"max_tokens", "max_output_tokens", "length"}
@@ -532,25 +556,13 @@ def run_agent_task(
charged to ``budget`` as it ends, so nothing is lost when a later attempt raises."""
before = ws.head()
timeout = task.timeout_s or (settings.limits.agent_timeout_s if settings is not None else 1800)
- # ...but never longer than the run's remaining wall clock. The scene track has
- # clipped this since the greenhouse incident; object/articulated/graphics did not,
- # so a flat 30-minute session ran 9 minutes PAST a 30-minute ceiling (measured
- # 2026-08-27: 'elapsed 39.0 min exceeds max_minutes 30.0', round 0 never finished).
- if budget is not None and hasattr(budget, "check"):
- # ...and a run past its HARD ceiling must not start one at all. A timed-out
- # session that produced nothing bills nothing, the accounting-driven check()
- # never fires, and the round loop treadmills on 120 s mercy floors forever
- # (measured 2026-08-28: lamp_bl, 30-min cap, killed by hand at 57 min with
- # round 0 unfinished — silent-bail retries kept getting floor sessions).
- budget.check()
- if budget is not None and hasattr(budget, "timeout_s"):
- # A task that chose its own window already clipped it the way its stage wanted
- # (scene.py: soft for env/zones/assets, hard for refine/rebuild) — re-clipping THAT
- # against the soft share handed every scene refine session after the 0.55 share
- # exactly the 120 s floor instead of the ≤ 900 s it asked for. A task with no
- # window of its own (repair) is still bounded by the soft share, so a failing
- # baseline cannot eat the refine rounds' half. (Both found by review 2026-08-29.)
- timeout = budget.timeout_s(timeout, floor_s=120.0, soft=task.timeout_s is None)
+ # A task that chose its own window already clipped it the way its stage wanted
+ # (scene.py: soft for env/zones/assets, hard for refine/rebuild) — re-clipping THAT
+ # against the soft share handed every scene refine session after the 0.55 share
+ # exactly the 120 s floor instead of the ≤ 900 s it asked for. A task with no
+ # window of its own (repair) is still bounded by the soft share, so a failing
+ # baseline cannot eat the refine rounds' half. (Both found by review 2026-08-29.)
+ timeout = _deadline_preflight(budget, timeout, soft=task.timeout_s is None)
turns_cap = task.max_turns or max_turns or agent_max_turns() # 0 = leave AgentJob's own default
# typed job context honoured by every CodingAgent: round → trajectory dir + ToolContext,
# language/track → spatial tool filtering, files_hint → the edit_only scope.
diff --git a/harness/tests/orchestrator_tracks/test_budget_snapshot.py b/harness/tests/orchestrator_tracks/test_budget_snapshot.py
index 0a0ce7b0..451aab0e 100644
--- a/harness/tests/orchestrator_tracks/test_budget_snapshot.py
+++ b/harness/tests/orchestrator_tracks/test_budget_snapshot.py
@@ -127,11 +127,15 @@ def test_the_texture_pass_spend_reaches_the_snapshot_a_resume_restores(tmp_path,
def test_a_paid_single_shot_response_is_persisted_when_the_budget_trips(tmp_path):
ws = Workspace(tmp_path / "ws").create()
guard = BudgetGuard(Budget(max_minutes=1.0))
- guard._active_s = 120.0 # noqa: SLF001 — already past 1 min
answer = ("=== FILE: src/object.js ===\n"
"export function build(THREE) { return new THREE.Group(); }\n"
"=== END FILE ===")
- model = FakeChatModel(lambda req: answer, cost=0.05) # paid work, booked before the stop
+
+ def slow_call(req): # the CALL ITSELF crosses the wall-clock ceiling
+ guard._active_s = 120.0 # noqa: SLF001
+ return answer
+
+ model = FakeChatModel(slow_call, cost=0.05) # paid work, booked before the stop
task = GenerationTask(label="baseline", prompt="p", files_hint=["src/object.js"])
res = generate_files(ws, model=model, task=task, budget=guard) # must NOT raise
assert res.ok and (ws.src / "object.js").is_file()
@@ -140,6 +144,52 @@ def test_a_paid_single_shot_response_is_persisted_when_the_budget_trips(tmp_path
assert not guard.ok() # the phase boundary (steps._run_phase) turns this into the stop
+def test_single_shot_obeys_the_run_clock(tmp_path):
+ """A run past its HARD wall-clock ceiling must not buy a single-shot call at all
+ (agent-path symmetry), and a live run's request carries the REMAINING wall clock
+ as max_wait_s — never the models' 1800 s retry default. The truncation retry is
+ a second full-price call: it is preflighted the same way."""
+ ws = Workspace(tmp_path / "ws").create()
+ task = GenerationTask(label="baseline", prompt="p", files_hint=["src/object.js"], max_output_tokens=1000)
+
+ calls: list = []
+ answer = ("=== FILE: src/object.js ===\n"
+ "export function build(THREE) { return new THREE.Group(); }\n"
+ "=== END FILE ===")
+ model = FakeChatModel(lambda req: (calls.append(req) or answer), cost=0.01)
+
+ past = BudgetGuard(Budget(max_minutes=1.0))
+ past._active_s = 120.0 # noqa: SLF001 — already past 1 min
+ with pytest.raises(BudgetExceeded):
+ generate_files(ws, model=model, task=task, budget=past)
+ assert calls == [] and past.spent.cost_usd == 0.0 # refused BEFORE any model call
+
+ live = BudgetGuard(Budget(max_minutes=10.0))
+ live._active_s = 8 * 60.0 # noqa: SLF001 — 2 minutes left
+ res = generate_files(ws, model=model, task=task, budget=live)
+ assert res.ok and len(calls) == 1
+ assert calls[0].max_wait_s is not None and calls[0].max_wait_s <= 121 # clipped to remaining clock
+
+ # truncation retry: the first response is truncated, then the clock runs out
+ class TruncatingModel:
+ def __init__(self):
+ self.calls = 0
+
+ def generate(self, req):
+ from codeverse.contracts.chat import ChatResponse
+ self.calls += 1
+ live2._active_s = 11 * 60.0 # noqa: SLF001 — ceiling crossed mid-call
+ return ChatResponse(text="=== FILE: src/object.js ===\nx", finish_reason="max_tokens",
+ usage=Usage(backend="fake", cost_usd=0.01), raw={})
+
+ live2 = BudgetGuard(Budget(max_minutes=10.0))
+ tm = TruncatingModel()
+ with pytest.raises(BudgetExceeded):
+ generate_files(ws, model=tm, task=task, budget=live2)
+ assert tm.calls == 1 # the retry was never bought
+ assert live2.spent.cost_usd == pytest.approx(0.01) # ...but the paid first call is booked
+
+
# --------------------------------------------------------------- ordering: fan-out siblings
def test_a_budget_tripped_candidate_still_lets_the_sibling_be_adopted(tmp_path, chair_plan, settings):
spec = make_spec(max_rounds=0)