diff --git a/loopx/control_plane/testing/capability_monitor_repair_tool_behavior.py b/loopx/control_plane/testing/capability_monitor_repair_tool_behavior.py index 3530bac530..b38a7317d0 100644 --- a/loopx/control_plane/testing/capability_monitor_repair_tool_behavior.py +++ b/loopx/control_plane/testing/capability_monitor_repair_tool_behavior.py @@ -5,27 +5,22 @@ import shlex import subprocess from collections.abc import Mapping -from hashlib import sha256 from pathlib import Path from typing import Any from ...heartbeat_prompt import build_heartbeat_prompt -from ..quota.turn_envelope import quota_action_signature_document -from .doubao_model_behavior_actor import ( - ARK_API_KEY_ENV, - DOUBAO_2_1_PRO_MODEL, - DOUBAO_MODEL_ENV, - DoubaoActorTransport, - _direct_ark_transport, -) from .model_tool_behavior import ( + DoubaoExecToolBehaviorActorBase, QuotaToolContext, - is_context_read_command, - DoubaoExecToolClient, + append_exec_tool_exchange, argument_value, + build_qualification_receipt, digest_text, execute_loopx_cli, + extract_quota_action_contract_base, + is_context_read_command, loopx_command_tokens, + qualification_turn_instance_id, ) from .selected_todo_tool_behavior import ( SELECTED_TODO_TOOL_FIXTURE_AGENT_ID, @@ -309,9 +304,7 @@ def _redacted_command_shape( def _capability_repair_contract(packet: Mapping[str, Any]) -> dict[str, Any]: - signature = quota_action_signature_document(packet) - action = dict(signature.get("action") or {}) - user = dict(signature.get("user") or {}) + base, _, _, _ = extract_quota_action_contract_base(packet) interaction = dict(packet.get("interaction_contract") or {}) capability_gate = dict(packet.get("capability_gate") or {}) cli_channel = dict(interaction.get("cli_channel") or {}) @@ -331,13 +324,8 @@ def _capability_repair_contract(packet: Mapping[str, Any]) -> dict[str, Any]: item for item in next_cli_actions if _is_capability_reentry_command(item) ] contract = { - "decision": "execute", + **base, "selected_todo_id": None, - "user_action_required": bool(user.get("action_required")), - "must_attempt_work": bool(action.get("must_attempt")), - "delivery_allowed": bool(action.get("delivery_allowed")), - "quiet_noop_allowed": bool(action.get("quiet_noop_allowed")), - "external_write_requested": False, "repair_missing": list(capability_gate.get("repair_missing") or []), } if not ( @@ -387,9 +375,7 @@ def _execute_capability_callsite( def _capability_reentry_contract(packet: Mapping[str, Any]) -> dict[str, Any]: - signature = quota_action_signature_document(packet) - action = dict(signature.get("action") or {}) - selected = dict(action.get("selected_todo") or {}) + _, _, action, selected = extract_quota_action_contract_base(packet) interaction = dict(packet.get("interaction_contract") or {}) capability_gate = dict(packet.get("capability_gate") or {}) if not ( @@ -419,72 +405,32 @@ def _receipt( passed: bool, failure_code: str | None, ) -> dict[str, Any]: - return { - "schema_version": CAPABILITY_MONITOR_REPAIR_TOOL_BEHAVIOR_RECEIPT_SCHEMA_VERSION, - "qualification_id": qualification_id, - "actor_ref": actor_ref, - "qualification_passed": passed, - "failure_code": failure_code, - **dict(contract or {}), - "observed_tool_sequence": [step["kind"] for step in steps], - "tool_call_count": len(steps), - "capability_callsite_verified": callsite_verified, - "same_turn_quota_reentry_completed": reentry_completed, - "monitor_fallback_avoided": reentry_completed, - "tool_call_receipts": steps, - "boundary": { - "raw_prompt_persisted": False, - "raw_provider_response_persisted": False, - "raw_command_persisted": False, - "writes_limited_to_temporary_fixture": True, - "external_writes_executed": False, - "shell_commands_executed": False, + return build_qualification_receipt( + schema_version=CAPABILITY_MONITOR_REPAIR_TOOL_BEHAVIOR_RECEIPT_SCHEMA_VERSION, + qualification_id=qualification_id, + actor_ref=actor_ref, + passed=passed, + failure_code=failure_code, + steps=steps, + contract=contract, + extra_fields={ + "capability_callsite_verified": callsite_verified, + "same_turn_quota_reentry_completed": reentry_completed, + "monitor_fallback_avoided": reentry_completed, + }, + boundary={ "task_facing_reads_executed": callsite_verified, "control_plane_writes_executed": False, "advancement_checkpoints_written": False, }, - } + ) -class DoubaoCapabilityMonitorRepairToolBehaviorActor: +class DoubaoCapabilityMonitorRepairToolBehaviorActor( + DoubaoExecToolBehaviorActorBase +): """Prove inline capability re-entry outranks a monitor fallback.""" - def __init__( - self, - *, - api_key: str, - model: str = DOUBAO_2_1_PRO_MODEL, - timeout_seconds: float = 90.0, - transport: DoubaoActorTransport = _direct_ark_transport, - ) -> None: - self._client = DoubaoExecToolClient( - api_key=api_key, - model=model, - timeout_seconds=timeout_seconds, - transport=transport, - ) - - @classmethod - def from_environment( - cls, - *, - environ: Mapping[str, str] | None = None, - transport: DoubaoActorTransport = _direct_ark_transport, - timeout_seconds: float = 90.0, - ) -> DoubaoCapabilityMonitorRepairToolBehaviorActor: - values = os.environ if environ is None else environ - api_key = values.get(ARK_API_KEY_ENV, "") - if not api_key.strip(): - raise RuntimeError( - "ARK_API_KEY is not injected; live Doubao qualification is unavailable" - ) - return cls( - api_key=api_key, - model=values.get(DOUBAO_MODEL_ENV, DOUBAO_2_1_PRO_MODEL), - timeout_seconds=timeout_seconds, - transport=transport, - ) - def qualify( self, *, @@ -525,8 +471,7 @@ def qualify( contract: dict[str, Any] | None = None seen_once: set[str] = set() actor_ref = self._client.actor_ref - qualification_digest = sha256(qualification_id.encode()).hexdigest()[:16] - turn_instance_id = f"qualification-{qualification_digest}" + turn_instance_id = qualification_turn_instance_id(qualification_id) def receipt(*, passed: bool, failure_code: str | None) -> dict[str, Any]: return _receipt( @@ -678,19 +623,6 @@ def receipt(*, passed: bool, failure_code: str | None) -> dict[str, Any]: passed=False, failure_code="workspace_read_failed", ) - messages.extend( - [ - { - "role": "assistant", - "content": tool_call.assistant_content, - "tool_calls": [tool_call.provider_value], - }, - { - "role": "tool", - "tool_call_id": tool_call.call_id, - "content": tool_output or "", - }, - ] - ) + append_exec_tool_exchange(messages, tool_call, tool_output) return receipt(passed=False, failure_code="tool_call_budget_exhausted") diff --git a/loopx/control_plane/testing/model_tool_behavior.py b/loopx/control_plane/testing/model_tool_behavior.py index caa9d80203..cb0ca3d5de 100644 --- a/loopx/control_plane/testing/model_tool_behavior.py +++ b/loopx/control_plane/testing/model_tool_behavior.py @@ -10,13 +10,18 @@ from dataclasses import dataclass from hashlib import sha256 from pathlib import Path -from typing import Any +from typing import Any, Self +from ..quota.turn_envelope import quota_action_signature_document from .doubao_model_behavior_actor import ( ALLOWED_MODEL_BEHAVIOR_MODELS, + ARK_API_KEY_ENV, + DOUBAO_2_1_PRO_MODEL, DOUBAO_CHAT_COMPLETIONS_ENDPOINT, + DOUBAO_MODEL_ENV, DoubaoActorTransport, DoubaoActorTransportError, + _direct_ark_transport, ) EXEC_COMMAND_TOOL: dict[str, Any] = { @@ -710,6 +715,225 @@ def __init__(self, returncode: int, detail: str, *, stdout: str = "") -> None: self.stdout = stdout +def qualification_turn_instance_id(qualification_id: str) -> str: + """Derive a deterministic Turn instance id for a hermetic qualification.""" + + digest = sha256(qualification_id.encode("utf-8")).hexdigest()[:16] + return f"qualification-{digest}" + + +def hermetic_clock_output( + command: str, + *, + utc_output: str, + local_output: str, +) -> str | None: + """Return fixed host clock output for bounded `date` invocations only.""" + + try: + tokens = shlex.split(command) + except ValueError: + return None + if not tokens or Path(tokens[0]).name != "date" or len(tokens) > 4: + return None + if any( + token in {";", "&&", "||", "|"} + or (index > 0 and not token.startswith(("-", "+"))) + for index, token in enumerate(tokens) + ): + return None + if "-u" in tokens or "--utc" in tokens: + return utc_output + return local_output + + +def is_fixture_quota_guard( + command: str, + *, + goal_id: str, + agent_id: str, +) -> bool: + """Return whether ``command`` invokes the fixture's `quota should-run` guard.""" + + tokens = loopx_command_tokens(command) + if not tokens: + return False + try: + quota_index = tokens.index("quota") + except ValueError: + return False + return bool( + tokens[quota_index : quota_index + 2] == ["quota", "should-run"] + and argument_value(tokens, "--goal-id") == goal_id + and argument_value(tokens, "--agent-id") == agent_id + and "--codex-app" in tokens + and argument_value(tokens, "--turn-instance-id") + ) + + +def extract_quota_action_contract_base( + packet: Mapping[str, Any], +) -> tuple[dict[str, Any], dict[str, Any], dict[str, Any], dict[str, Any]]: + """Extract the shared action signature summary used by qualification contracts.""" + + signature = quota_action_signature_document(packet) + action = dict(signature.get("action") or {}) + user = dict(signature.get("user") or {}) + selected = dict(action.get("selected_todo") or {}) + contract_base: dict[str, Any] = { + "decision": "execute", + "selected_todo_id": selected.get("todo_id"), + "user_action_required": bool(user.get("action_required")), + "must_attempt_work": bool(action.get("must_attempt")), + "delivery_allowed": bool(action.get("delivery_allowed")), + "quiet_noop_allowed": bool(action.get("quiet_noop_allowed")), + "external_write_requested": False, + } + return contract_base, signature, action, selected + + +def execute_bounded_read_steps( + plan: Sequence[Any], + *, + cwd: Path, + timeout_seconds: float = 10, +) -> tuple[list[tuple[Any, int, str]], int]: + """Run parsed bounded-read steps under shell `;`/`&&`/`||` status rules.""" + + results: list[tuple[Any, int, str]] = [] + status = 0 + for step in plan: + should_run = bool( + step.operator == ";" + or (step.operator == "&&" and status == 0) + or (step.operator == "||" and status != 0) + ) + if not should_run: + continue + completed = subprocess.run( + list(step.argv), + cwd=cwd, + check=False, + capture_output=True, + text=True, + encoding="utf-8", + errors="replace", + timeout=timeout_seconds, + ) + status = completed.returncode + output = completed.stdout + if step.line_limit is not None: + output = "".join(output.splitlines(keepends=True)[: step.line_limit]) + results.append((step, status, output)) + return results, status + + +def build_qualification_receipt( + *, + schema_version: str, + qualification_id: str, + actor_ref: str, + passed: bool, + failure_code: str | None, + steps: Sequence[Mapping[str, Any]], + contract: Mapping[str, Any] | None = None, + extra_fields: Mapping[str, Any] | None = None, + boundary: Mapping[str, Any] | None = None, +) -> dict[str, Any]: + """Assemble the standard redacted qualification receipt payload.""" + + merged_boundary: dict[str, Any] = { + "raw_prompt_persisted": False, + "raw_provider_response_persisted": False, + "raw_command_persisted": False, + "writes_limited_to_temporary_fixture": True, + "external_writes_executed": False, + "shell_commands_executed": False, + **(dict(boundary) if boundary is not None else {}), + } + receipt: dict[str, Any] = { + "schema_version": schema_version, + "qualification_id": qualification_id, + "actor_ref": actor_ref, + "qualification_passed": passed, + "failure_code": failure_code, + **dict(contract or {}), + "observed_tool_sequence": [step["kind"] for step in steps], + "tool_call_count": len(steps), + **dict(extra_fields or {}), + "tool_call_receipts": list(steps), + "boundary": merged_boundary, + } + return receipt + + +def append_exec_tool_exchange( + messages: list[dict[str, Any]], + tool_call: ExecToolCall, + tool_output: str | None, + *, + include_assistant_content: bool = True, +) -> None: + """Append the assistant exec tool request and tool response to ``messages``.""" + + messages.extend( + [ + { + "role": "assistant", + "content": ( + tool_call.assistant_content if include_assistant_content else None + ), + "tool_calls": [tool_call.provider_value], + }, + { + "role": "tool", + "tool_call_id": tool_call.call_id, + "content": "" if tool_output is None else tool_output, + }, + ] + ) + + +class DoubaoExecToolBehaviorActorBase: + """Shared lifecycle initialization for exec-tool Doubao behavior actors.""" + + def __init__( + self, + *, + api_key: str, + model: str = DOUBAO_2_1_PRO_MODEL, + timeout_seconds: float = 90.0, + transport: DoubaoActorTransport = _direct_ark_transport, + ) -> None: + self._client = DoubaoExecToolClient( + api_key=api_key, + model=model, + timeout_seconds=timeout_seconds, + transport=transport, + ) + + @classmethod + def from_environment( + cls, + *, + environ: Mapping[str, str] | None = None, + transport: DoubaoActorTransport = _direct_ark_transport, + timeout_seconds: float = 90.0, + ) -> Self: + values = os.environ if environ is None else environ + api_key = values.get(ARK_API_KEY_ENV, "") + if not api_key.strip(): + raise RuntimeError( + "ARK_API_KEY is not injected; live Doubao qualification is unavailable" + ) + return cls( + api_key=api_key, + model=values.get(DOUBAO_MODEL_ENV, DOUBAO_2_1_PRO_MODEL), + timeout_seconds=timeout_seconds, + transport=transport, + ) + + def snapshot_disposable_fixture(root: Path) -> dict[str, tuple[Any, ...]]: """Observe all fixture files, never infer production rollback from stdout. diff --git a/loopx/control_plane/testing/replan_semantic_action_behavior.py b/loopx/control_plane/testing/replan_semantic_action_behavior.py index 19f771cb77..0ef7843527 100644 --- a/loopx/control_plane/testing/replan_semantic_action_behavior.py +++ b/loopx/control_plane/testing/replan_semantic_action_behavior.py @@ -3,7 +3,6 @@ import json import os import shlex -import subprocess from collections.abc import Callable, Mapping from dataclasses import dataclass from hashlib import sha256 @@ -17,23 +16,23 @@ ProgressResultClass, semantic_delta_from_writeback, ) -from .doubao_model_behavior_actor import ( - ARK_API_KEY_ENV, - DOUBAO_2_1_PRO_MODEL, - DOUBAO_MODEL_ENV, - DoubaoActorTransport, - _direct_ark_transport, -) from .model_tool_behavior import ( _module_reader_tokens, - QuotaToolContext, - is_context_read_command, - LoopxCliExecutionError, + DoubaoExecToolBehaviorActorBase, DoubaoExecToolClient, + LoopxCliExecutionError, + QuotaToolContext, + append_exec_tool_exchange, argument_value, + build_qualification_receipt, digest_text, + execute_bounded_read_steps, execute_loopx_cli, + hermetic_clock_output, + is_context_read_command, + is_fixture_quota_guard, loopx_command_tokens, + qualification_turn_instance_id, replan_view_command_tokens, ) from .selected_todo_tool_behavior import bounded_workspace_read_plan @@ -521,37 +520,18 @@ def _build_fixture( def _clock_output(command: str) -> str | None: - try: - tokens = shlex.split(command) - except ValueError: - return None - if not tokens or Path(tokens[0]).name != "date" or len(tokens) > 4: - return None - if any( - token in {";", "&&", "||", "|"} - or (index > 0 and not token.startswith(("-", "+"))) - for index, token in enumerate(tokens) - ): - return None - if "-u" in tokens or "--utc" in tokens: - return "2026-08-12T16:00:00Z\n" - return "2026-08-13T00:00:00+08:00\n" + return hermetic_clock_output( + command, + utc_output="2026-08-12T16:00:00Z\n", + local_output="2026-08-13T00:00:00+08:00\n", + ) def _is_quota_guard(command: str) -> bool: - tokens = loopx_command_tokens(command) - if not tokens: - return False - try: - quota_index = tokens.index("quota") - except ValueError: - return False - return bool( - tokens[quota_index : quota_index + 2] == ["quota", "should-run"] - and argument_value(tokens, "--goal-id") == _FIXTURE_GOAL_ID - and argument_value(tokens, "--agent-id") == _FIXTURE_AGENT_ID - and "--codex-app" in tokens - and argument_value(tokens, "--turn-instance-id") + return is_fixture_quota_guard( + command, + goal_id=_FIXTURE_GOAL_ID, + agent_id=_FIXTURE_AGENT_ID, ) @@ -939,44 +919,22 @@ def _execute_workspace_read( plan = _bounded_workspace_read_plan(command, fixture=fixture) if plan is None: raise ValueError("workspace read is outside the hermetic fixture") - outputs: list[str] = [] - status = 0 - frontier_read = False - work_source_read = False - for step in plan: - should_run = bool( - step.operator == ";" - or (step.operator == "&&" and status == 0) - or (step.operator == "||" and status != 0) - ) - if not should_run: - continue - completed = subprocess.run( - list(step.argv), - cwd=fixture.project_root, - check=False, - capture_output=True, - text=True, encoding="utf-8", errors="replace", - timeout=10, - ) - status = completed.returncode - output = completed.stdout - if step.line_limit is not None: - output = "".join(output.splitlines(keepends=True)[: step.line_limit]) - outputs.append(output) - frontier_read = frontier_read or ( - status == 0 - and step.target is not None - and step.target.resolve() == fixture.frontier_target.resolve() - ) - work_source_read = work_source_read or ( - status == 0 - and step.target is not None - and step.target.resolve() == fixture.work_source_target.resolve() - ) + results, status = execute_bounded_read_steps(plan, cwd=fixture.project_root) + frontier_read = any( + step_status == 0 + and step.target is not None + and step.target.resolve() == fixture.frontier_target.resolve() + for step, step_status, _ in results + ) + work_source_read = any( + step_status == 0 + and step.target is not None + and step.target.resolve() == fixture.work_source_target.resolve() + for step, step_status, _ in results + ) if status != 0: raise RuntimeError(f"workspace read failed with exit={status}") - return "".join(outputs), frontier_read, work_source_read + return "".join(output for _, _, output in results), frontier_read, work_source_read def _receipt( @@ -991,32 +949,27 @@ def _receipt( local_fixture_writes_executed: bool, read_only_host_commands_executed: bool, ) -> dict[str, Any]: - receipt = { - "schema_version": REPLAN_SEMANTIC_ACTION_BEHAVIOR_RECEIPT_SCHEMA_VERSION, - "qualification_id": qualification_id, - "actor_ref": actor_ref, - "qualification_passed": passed, - "failure_code": failure_code, - "observed_tool_sequence": [step["kind"] for step in steps], - "tool_call_count": len(steps), - "semantic_action_accepted": bool( - passed and semantic_delta and semantic_delta.get("accepted") is True - ), - "selected_semantic_outcomes": list( - (semantic_delta or {}).get("satisfying_outcomes") or [] - ), - "tool_call_receipts": steps, - "boundary": { - "raw_prompt_persisted": False, - "raw_provider_response_persisted": False, - "raw_command_persisted": False, + receipt = build_qualification_receipt( + schema_version=REPLAN_SEMANTIC_ACTION_BEHAVIOR_RECEIPT_SCHEMA_VERSION, + qualification_id=qualification_id, + actor_ref=actor_ref, + passed=passed, + failure_code=failure_code, + steps=steps, + extra_fields={ + "semantic_action_accepted": bool( + passed and semantic_delta and semantic_delta.get("accepted") is True + ), + "selected_semantic_outcomes": list( + (semantic_delta or {}).get("satisfying_outcomes") or [] + ), + }, + boundary={ "filesystem_writes_executed": local_fixture_writes_executed, "writes_limited_to_temporary_fixture": local_fixture_writes_executed, - "external_writes_executed": False, - "shell_commands_executed": False, "read_only_host_commands_executed": read_only_host_commands_executed, }, - } + ) if quota_observation is not None: receipt.update(dict(quota_observation)) return receipt @@ -1655,19 +1608,8 @@ def _append_tool_response( tool_call: Any, output: str, ) -> None: - state.messages.extend( - [ - { - "role": "assistant", - "content": None, - "tool_calls": [tool_call.provider_value], - }, - { - "role": "tool", - "tool_call_id": tool_call.call_id, - "content": output, - }, - ] + append_exec_tool_exchange( + state.messages, tool_call, output, include_assistant_content=False ) @@ -1791,45 +1733,9 @@ def _run_qualification_loop( ) -class DoubaoReplanSemanticActionBehaviorActor: +class DoubaoReplanSemanticActionBehaviorActor(DoubaoExecToolBehaviorActorBase): """Qualify a model-selected semantic replan through the real CLI boundary.""" - def __init__( - self, - *, - api_key: str, - model: str = DOUBAO_2_1_PRO_MODEL, - timeout_seconds: float = 90.0, - transport: DoubaoActorTransport = _direct_ark_transport, - ) -> None: - self._client = DoubaoExecToolClient( - api_key=api_key, - model=model, - timeout_seconds=timeout_seconds, - transport=transport, - ) - - @classmethod - def from_environment( - cls, - *, - environ: Mapping[str, str] | None = None, - transport: DoubaoActorTransport = _direct_ark_transport, - timeout_seconds: float = 90.0, - ) -> DoubaoReplanSemanticActionBehaviorActor: - values = os.environ if environ is None else environ - api_key = values.get(ARK_API_KEY_ENV, "") - if not api_key.strip(): - raise RuntimeError( - "ARK_API_KEY is not injected; live Doubao qualification is unavailable" - ) - return cls( - api_key=api_key, - model=values.get(DOUBAO_MODEL_ENV, DOUBAO_2_1_PRO_MODEL), - timeout_seconds=timeout_seconds, - transport=transport, - ) - def qualify( self, *, @@ -1882,12 +1788,11 @@ def qualify( }, {"role": "user", "content": fixture.task_body}, ] - digest = sha256(qualification_id.encode()).hexdigest()[:16] state = _QualificationState( fixture=fixture, messages=messages, steps=[], - turn_instance_id=f"qualification-{digest}", + turn_instance_id=qualification_turn_instance_id(qualification_id), ) if required_vision: def invoke(argv: list[str], cwd: Path, output: str) -> str: diff --git a/loopx/control_plane/testing/scoped_gate_successor_tool_behavior.py b/loopx/control_plane/testing/scoped_gate_successor_tool_behavior.py index 0787ac018a..11301a51fc 100644 --- a/loopx/control_plane/testing/scoped_gate_successor_tool_behavior.py +++ b/loopx/control_plane/testing/scoped_gate_successor_tool_behavior.py @@ -5,26 +5,21 @@ import shlex import subprocess from collections.abc import Mapping -from hashlib import sha256 from pathlib import Path from typing import Any from ...heartbeat_prompt import build_heartbeat_prompt -from ..quota.turn_envelope import quota_action_signature_document -from .doubao_model_behavior_actor import ( - ARK_API_KEY_ENV, - DOUBAO_2_1_PRO_MODEL, - DOUBAO_MODEL_ENV, - DoubaoActorTransport, - _direct_ark_transport, -) from .model_tool_behavior import ( + DoubaoExecToolBehaviorActorBase, QuotaToolContext, - is_context_read_command, - DoubaoExecToolClient, QUOTA_FIRST_TOOL_INSTRUCTION, + append_exec_tool_exchange, + build_qualification_receipt, digest_text, execute_loopx_cli, + extract_quota_action_contract_base, + is_context_read_command, + qualification_turn_instance_id, ) from .selected_todo_tool_behavior import ( SELECTED_TODO_TOOL_FIXTURE_AGENT_ID, @@ -203,10 +198,7 @@ def _build_scoped_gate_fixture(root: Path) -> _SelectedTodoToolFixture: def _scoped_gate_quota_contract(packet: Mapping[str, Any]) -> dict[str, Any]: - signature = quota_action_signature_document(packet) - action = dict(signature.get("action") or {}) - user = dict(signature.get("user") or {}) - selected = dict(action.get("selected_todo") or {}) + contract, signature, _, _ = extract_quota_action_contract_base(packet) interaction = dict(packet.get("interaction_contract") or {}) user_channel = dict(interaction.get("user_channel") or {}) actions = [ @@ -214,15 +206,6 @@ def _scoped_gate_quota_contract(packet: Mapping[str, Any]) -> dict[str, Any]: for item in user_channel.get("actions") or [] if str(item).strip() ] - contract = { - "decision": "execute", - "selected_todo_id": selected.get("todo_id"), - "user_action_required": bool(user.get("action_required")), - "must_attempt_work": bool(action.get("must_attempt")), - "delivery_allowed": bool(action.get("delivery_allowed")), - "quiet_noop_allowed": bool(action.get("quiet_noop_allowed")), - "external_write_requested": False, - } if not ( contract["selected_todo_id"] == SCOPED_GATE_SUCCESSOR_TODO_ID and contract["user_action_required"] @@ -289,72 +272,30 @@ def _receipt( passed: bool, failure_code: str | None, ) -> dict[str, Any]: - return { - "schema_version": SCOPED_GATE_SUCCESSOR_TOOL_BEHAVIOR_RECEIPT_SCHEMA_VERSION, - "qualification_id": qualification_id, - "actor_ref": actor_ref, - "qualification_passed": passed, - "failure_code": failure_code, - **dict(contract or {}), - "observed_tool_sequence": [step["kind"] for step in steps], - "tool_call_count": len(steps), - "tool_call_limit": SCOPED_GATE_SUCCESSOR_TOOL_BEHAVIOR_MAX_CALLS, - "non_blocking_notice_surfaced": notice_surfaced, - "selected_action_matched_todo": selected_action_executed, - "tool_call_receipts": steps, - "boundary": { - "raw_prompt_persisted": False, - "raw_provider_response_persisted": False, + return build_qualification_receipt( + schema_version=SCOPED_GATE_SUCCESSOR_TOOL_BEHAVIOR_RECEIPT_SCHEMA_VERSION, + qualification_id=qualification_id, + actor_ref=actor_ref, + passed=passed, + failure_code=failure_code, + steps=steps, + contract=contract, + extra_fields={ + "tool_call_limit": SCOPED_GATE_SUCCESSOR_TOOL_BEHAVIOR_MAX_CALLS, + "non_blocking_notice_surfaced": notice_surfaced, + "selected_action_matched_todo": selected_action_executed, + }, + boundary={ "raw_assistant_content_persisted": False, - "raw_command_persisted": False, "filesystem_writes_executed": True, - "writes_limited_to_temporary_fixture": True, - "external_writes_executed": False, - "shell_commands_executed": False, "read_only_host_commands_executed": bool(steps), }, - } + ) -class DoubaoScopedGateSuccessorToolBehaviorActor: +class DoubaoScopedGateSuccessorToolBehaviorActor(DoubaoExecToolBehaviorActorBase): """Prove a scoped user notice does not suppress its ready successor.""" - def __init__( - self, - *, - api_key: str, - model: str = DOUBAO_2_1_PRO_MODEL, - timeout_seconds: float = 90.0, - transport: DoubaoActorTransport = _direct_ark_transport, - ) -> None: - self._client = DoubaoExecToolClient( - api_key=api_key, - model=model, - timeout_seconds=timeout_seconds, - transport=transport, - ) - - @classmethod - def from_environment( - cls, - *, - environ: Mapping[str, str] | None = None, - transport: DoubaoActorTransport = _direct_ark_transport, - timeout_seconds: float = 90.0, - ) -> DoubaoScopedGateSuccessorToolBehaviorActor: - values = os.environ if environ is None else environ - api_key = values.get(ARK_API_KEY_ENV, "") - if not api_key.strip(): - raise RuntimeError( - "ARK_API_KEY is not injected; live Doubao qualification is unavailable" - ) - return cls( - api_key=api_key, - model=values.get(DOUBAO_MODEL_ENV, DOUBAO_2_1_PRO_MODEL), - timeout_seconds=timeout_seconds, - transport=transport, - ) - def qualify( self, *, @@ -392,8 +333,7 @@ def qualify( seen_once: set[str] = set() rejected_read_count = 0 actor_ref = self._client.actor_ref - qualification_digest = sha256(qualification_id.encode()).hexdigest()[:16] - turn_instance_id = f"qualification-{qualification_digest}" + turn_instance_id = qualification_turn_instance_id(qualification_id) def receipt(*, passed: bool, failure_code: str | None) -> dict[str, Any]: return _receipt( @@ -509,20 +449,7 @@ def receipt(*, passed: bool, failure_code: str | None) -> dict[str, Any]: passed=False, failure_code="workspace_read_failed", ) - messages.extend( - [ - { - "role": "assistant", - "content": tool_call.assistant_content, - "tool_calls": [tool_call.provider_value], - }, - { - "role": "tool", - "tool_call_id": tool_call.call_id, - "content": tool_output or "", - }, - ] - ) + append_exec_tool_exchange(messages, tool_call, tool_output) if selected_action_executed: # Finalization has no tools and is owed even when the selected # read used the last permitted tool call. diff --git a/loopx/control_plane/testing/selected_todo_tool_behavior.py b/loopx/control_plane/testing/selected_todo_tool_behavior.py index 896bea6bc3..c85f32cd7a 100644 --- a/loopx/control_plane/testing/selected_todo_tool_behavior.py +++ b/loopx/control_plane/testing/selected_todo_tool_behavior.py @@ -6,28 +6,26 @@ import subprocess from collections.abc import Mapping from dataclasses import dataclass -from hashlib import sha256 from pathlib import Path from typing import Any from ...heartbeat_prompt import build_heartbeat_prompt -from ..quota.turn_envelope import quota_action_signature_document -from .doubao_model_behavior_actor import ( - ARK_API_KEY_ENV, - DOUBAO_2_1_PRO_MODEL, - DOUBAO_MODEL_ENV, - DoubaoActorTransport, - _direct_ark_transport, -) from .model_tool_behavior import ( + DoubaoExecToolBehaviorActorBase, QuotaToolContext, - is_context_read_command, - DoubaoExecToolClient, QUOTA_FIRST_TOOL_INSTRUCTION, + append_exec_tool_exchange, argument_value, + build_qualification_receipt, digest_text, + execute_bounded_read_steps, execute_loopx_cli, + extract_quota_action_contract_base, + hermetic_clock_output, + is_context_read_command, + is_fixture_quota_guard, loopx_command_tokens, + qualification_turn_instance_id, ) SELECTED_TODO_TOOL_BEHAVIOR_RECEIPT_SCHEMA_VERSION = ( @@ -285,39 +283,18 @@ def _build_fixture( def _clock_output(command: str) -> str | None: - try: - tokens = shlex.split(command) - except ValueError: - return None - if not tokens or Path(tokens[0]).name != "date" or len(tokens) > 4: - return None - if any( - token in {";", "&&", "||", "|"} - or (index > 0 and not token.startswith(("-", "+"))) - for index, token in enumerate(tokens) - ): - return None - if "-u" in tokens or "--utc" in tokens: - return "2026-08-11T16:00:00Z\n" - return "2026-08-12T00:00:00+08:00\n" + return hermetic_clock_output( + command, + utc_output="2026-08-11T16:00:00Z\n", + local_output="2026-08-12T00:00:00+08:00\n", + ) def _is_quota_guard(command: str) -> bool: - tokens = loopx_command_tokens(command) - if not tokens: - return False - try: - quota_index = tokens.index("quota") - except ValueError: - return False - return bool( - tokens[quota_index : quota_index + 2] == ["quota", "should-run"] - and argument_value(tokens, "--goal-id") - == SELECTED_TODO_TOOL_FIXTURE_GOAL_ID - and argument_value(tokens, "--agent-id") - == SELECTED_TODO_TOOL_FIXTURE_AGENT_ID - and "--codex-app" in tokens - and argument_value(tokens, "--turn-instance-id") + return is_fixture_quota_guard( + command, + goal_id=SELECTED_TODO_TOOL_FIXTURE_GOAL_ID, + agent_id=SELECTED_TODO_TOOL_FIXTURE_AGENT_ID, ) @@ -816,35 +793,12 @@ def _execute_read_plan( *, fixture: _SelectedTodoToolFixture, ) -> tuple[str, int]: - outputs: list[str] = [] - status = 0 + results, status = execute_bounded_read_steps(plan, cwd=fixture.project_root) executed_content = 0 - for step in plan: - should_run = bool( - step.operator == ";" - or (step.operator == "&&" and status == 0) - or (step.operator == "||" and status != 0) - ) - if not should_run: - continue - completed = subprocess.run( - list(step.argv), - cwd=fixture.project_root, - check=False, - capture_output=True, - text=True, encoding="utf-8", errors="replace", - timeout=10, - ) - status = completed.returncode - step_output = completed.stdout - if step.line_limit is not None: - step_output = "".join( - step_output.splitlines(keepends=True)[: step.line_limit] - ) - outputs.append(step_output) + for step, step_status, step_output in results: if ( step.kind != "content" - or status != 0 + or step_status != 0 or step.target != fixture.selected_target.resolve() ): continue @@ -858,7 +812,7 @@ def _execute_read_plan( raise ValueError("selected todo readback does not match the fixture") if status != 0: raise RuntimeError(f"bounded read plan failed with exit={status}") - return "".join(outputs), executed_content + return "".join(output for _, _, output in results), executed_content def _execute_workspace_read( @@ -931,10 +885,7 @@ def _classify_tool_command( def _quota_contract(packet: Mapping[str, Any]) -> dict[str, Any]: - signature = quota_action_signature_document(packet) - action = dict(signature.get("action") or {}) - user = dict(signature.get("user") or {}) - selected = dict(action.get("selected_todo") or {}) + base, _, _, _ = extract_quota_action_contract_base(packet) action_portfolio_value = packet.get("action_portfolio") action_portfolio: Mapping[str, Any] = ( action_portfolio_value @@ -959,13 +910,7 @@ def _quota_contract(packet: Mapping[str, Any]) -> dict[str, Any]: else {} ) contract: dict[str, Any] = { - "decision": "execute", - "selected_todo_id": selected.get("todo_id"), - "user_action_required": bool(user.get("action_required")), - "must_attempt_work": bool(action.get("must_attempt")), - "delivery_allowed": bool(action.get("delivery_allowed")), - "quiet_noop_allowed": bool(action.get("quiet_noop_allowed")), - "external_write_requested": False, + **base, "selection_required": cli_channel.get("selection_required") is True, "suggested_action_ids": suggested_action_ids, } @@ -992,69 +937,25 @@ def _receipt( passed: bool, failure_code: str | None, ) -> dict[str, Any]: - return { - "schema_version": SELECTED_TODO_TOOL_BEHAVIOR_RECEIPT_SCHEMA_VERSION, - "qualification_id": qualification_id, - "actor_ref": actor_ref, - "qualification_passed": passed, - "failure_code": failure_code, - **dict(contract or {}), - "observed_tool_sequence": [step["kind"] for step in steps], - "tool_call_count": len(steps), - "selected_action_matched_todo": passed, - "tool_call_receipts": steps, - "boundary": { - "raw_prompt_persisted": False, - "raw_provider_response_persisted": False, - "raw_command_persisted": False, + return build_qualification_receipt( + schema_version=SELECTED_TODO_TOOL_BEHAVIOR_RECEIPT_SCHEMA_VERSION, + qualification_id=qualification_id, + actor_ref=actor_ref, + passed=passed, + failure_code=failure_code, + steps=steps, + contract=contract, + extra_fields={"selected_action_matched_todo": passed}, + boundary={ "filesystem_writes_executed": True, - "writes_limited_to_temporary_fixture": True, - "external_writes_executed": False, - "shell_commands_executed": False, "read_only_host_commands_executed": bool(steps), }, - } + ) -class DoubaoSelectedTodoToolBehaviorActor: +class DoubaoSelectedTodoToolBehaviorActor(DoubaoExecToolBehaviorActorBase): """Prove that the model acts on the Todo selected by real quota output.""" - def __init__( - self, - *, - api_key: str, - model: str = DOUBAO_2_1_PRO_MODEL, - timeout_seconds: float = 90.0, - transport: DoubaoActorTransport = _direct_ark_transport, - ) -> None: - self._client = DoubaoExecToolClient( - api_key=api_key, - model=model, - timeout_seconds=timeout_seconds, - transport=transport, - ) - - @classmethod - def from_environment( - cls, - *, - environ: Mapping[str, str] | None = None, - transport: DoubaoActorTransport = _direct_ark_transport, - timeout_seconds: float = 90.0, - ) -> DoubaoSelectedTodoToolBehaviorActor: - values = os.environ if environ is None else environ - api_key = values.get(ARK_API_KEY_ENV, "") - if not api_key.strip(): - raise RuntimeError( - "ARK_API_KEY is not injected; live Doubao qualification is unavailable" - ) - return cls( - api_key=api_key, - model=values.get(DOUBAO_MODEL_ENV, DOUBAO_2_1_PRO_MODEL), - timeout_seconds=timeout_seconds, - transport=transport, - ) - def qualify( self, *, @@ -1101,8 +1002,7 @@ def qualify( contract: dict[str, Any] | None = None seen_once: set[str] = set() actor_ref = self._client.actor_ref - qualification_digest = sha256(qualification_id.encode()).hexdigest()[:16] - turn_instance_id = f"qualification-{qualification_digest}" + turn_instance_id = qualification_turn_instance_id(qualification_id) def receipt(*, passed: bool, failure_code: str | None) -> dict[str, Any]: return _receipt( @@ -1229,19 +1129,8 @@ def receipt(*, passed: bool, failure_code: str | None) -> dict[str, Any]: passed=False, failure_code="workspace_read_failed", ) - messages.extend( - [ - { - "role": "assistant", - "content": None, - "tool_calls": [tool_call.provider_value], - }, - { - "role": "tool", - "tool_call_id": tool_call.call_id, - "content": tool_output or "", - }, - ] + append_exec_tool_exchange( + messages, tool_call, tool_output, include_assistant_content=False ) return receipt(passed=False, failure_code="tool_call_budget_exhausted") diff --git a/loopx/control_plane/testing/terminal_settlement_tool_behavior.py b/loopx/control_plane/testing/terminal_settlement_tool_behavior.py index 12218c2e9f..e2bbb962b9 100644 --- a/loopx/control_plane/testing/terminal_settlement_tool_behavior.py +++ b/loopx/control_plane/testing/terminal_settlement_tool_behavior.py @@ -15,24 +15,21 @@ from ..effect_program import SETTLEMENT_RECEIPT_SCHEMA_VERSION from ...heartbeat_prompt import build_heartbeat_prompt from ...control_plane.todos.mutation_api import add_goal_todo, complete_goal_todo -from ..quota.turn_envelope import quota_action_signature_document -from .doubao_model_behavior_actor import ( - ARK_API_KEY_ENV, - DOUBAO_2_1_PRO_MODEL, - DOUBAO_MODEL_ENV, - DoubaoActorTransport, - _direct_ark_transport, -) from .model_tool_behavior import ( + DoubaoExecToolBehaviorActorBase, + LoopxCliExecutionError, QuotaToolContext, - DoubaoExecToolClient, QUOTA_FIRST_TOOL_INSTRUCTION, - LoopxCliExecutionError, + append_exec_tool_exchange, argument_value, + build_qualification_receipt, digest_text, + execute_bounded_read_steps, execute_loopx_cli, + extract_quota_action_contract_base, is_context_read_command, loopx_command_tokens, + qualification_turn_instance_id, snapshot_disposable_fixture, ) from .selected_todo_tool_behavior import bounded_workspace_read_plan @@ -641,10 +638,7 @@ def _matches_identity(tokens: list[str], state: _QualificationState) -> bool: def _quota_contract(packet: Mapping[str, Any]) -> dict[str, Any]: - signature = quota_action_signature_document(packet) - action = dict(signature.get("action") or {}) - user = dict(signature.get("user") or {}) - selected = dict(action.get("selected_todo") or {}) + contract, _, _, _ = extract_quota_action_contract_base(packet) settlement_plan = dict( dict(packet.get("interaction_contract") or {}) .get("cli_channel", {}) @@ -657,15 +651,6 @@ def _quota_contract(packet: Mapping[str, Any]) -> dict[str, Any]: ] spend = next((item for item in settlement_plan.get("ordered_steps", []) if isinstance(item, Mapping) and item.get("kind") == "quota_spend"), {}) - contract = { - "decision": "execute", - "selected_todo_id": selected.get("todo_id"), - "user_action_required": bool(user.get("action_required")), - "must_attempt_work": bool(action.get("must_attempt")), - "delivery_allowed": bool(action.get("delivery_allowed")), - "quiet_noop_allowed": bool(action.get("quiet_noop_allowed")), - "external_write_requested": False, - } if ( contract["selected_todo_id"] != TERMINAL_SETTLEMENT_FIXTURE_TODO_ID or contract["user_action_required"] @@ -784,39 +769,17 @@ def _execute_workspace_read( ) if not plan: raise _TerminalToolViolation("command is not one bounded workspace read") - outputs: list[str] = [] - status = 0 - proof_read = False - for step in plan: - should_run = bool( - step.operator == ";" - or (step.operator == "&&" and status == 0) - or (step.operator == "||" and status != 0) - ) - if not should_run: - continue - completed = subprocess.run( - list(step.argv), - cwd=state.fixture.project_root, - check=False, - capture_output=True, - text=True, encoding="utf-8", errors="replace", - timeout=10, - ) - status = completed.returncode - output = completed.stdout - if step.line_limit is not None: - output = "".join(output.splitlines(keepends=True)[: step.line_limit]) - outputs.append(output) - proof_read = proof_read or bool( - status == 0 - and step.target is not None - and step.target.resolve() == state.fixture.proof_target.resolve() - ) + results, status = execute_bounded_read_steps(plan, cwd=state.fixture.project_root) + proof_read = any( + step_status == 0 + and step.target is not None + and step.target.resolve() == state.fixture.proof_target.resolve() + for step, step_status, _ in results + ) if status != 0: raise RuntimeError(f"workspace read failed with exit={status}") state.proof_read = state.proof_read or proof_read - return "".join(outputs) + return "".join(output for _, _, output in results) def _receipt( @@ -827,32 +790,27 @@ def _receipt( passed: bool, failure_code: str | None, ) -> dict[str, Any]: - return { - "schema_version": TERMINAL_SETTLEMENT_TOOL_BEHAVIOR_RECEIPT_SCHEMA_VERSION, - "qualification_id": qualification_id, - "actor_ref": actor_ref, - "qualification_passed": passed, - "failure_code": failure_code, - **dict(state.contract or {}), - "observed_tool_sequence": [step["kind"] for step in state.steps], - "tool_call_count": len(state.steps), - "tool_call_limit": TERMINAL_SETTLEMENT_TOOL_BEHAVIOR_MAX_CALLS, - "settlement_receipt_steps": list(state.receipt_steps), - "terminal_order_observed": bool( - passed and state.receipt_steps == _EXPECTED_RECEIPT_STEPS - ), - "tool_call_receipts": list(state.steps), - "boundary": { - "raw_prompt_persisted": False, - "raw_provider_response_persisted": False, - "raw_command_persisted": False, + return build_qualification_receipt( + schema_version=TERMINAL_SETTLEMENT_TOOL_BEHAVIOR_RECEIPT_SCHEMA_VERSION, + qualification_id=qualification_id, + actor_ref=actor_ref, + passed=passed, + failure_code=failure_code, + steps=state.steps, + contract=state.contract, + extra_fields={ + "tool_call_limit": TERMINAL_SETTLEMENT_TOOL_BEHAVIOR_MAX_CALLS, + "settlement_receipt_steps": list(state.receipt_steps), + "terminal_order_observed": bool( + passed and state.receipt_steps == _EXPECTED_RECEIPT_STEPS + ), + }, + boundary={ "filesystem_writes_executed": True, - "writes_limited_to_temporary_fixture": True, - "external_writes_executed": False, "shell_commands_executed": state.workspace_shell_executed, "read_only_host_commands_executed": True, }, - } + ) def _execute_reentry_cli( @@ -885,39 +843,33 @@ def _reentry_receipt( failure_code: str | None, ) -> dict[str, Any]: expected_count = len(state.fixture.projected_actions_by_todo) - return { - "schema_version": ( + return build_qualification_receipt( + schema_version=( TERMINAL_SETTLEMENT_REENTRY_TOOL_BEHAVIOR_RECEIPT_SCHEMA_VERSION ), - "qualification_id": qualification_id, - "actor_ref": actor_ref, - "scenario": "post_refresh_rejection_reentry", - "qualification_passed": passed, - "failure_code": failure_code, - "provider_call_count": state.provider_call_count, - "tool_call_count": len(state.steps), - "projected_todo_count": expected_count, - "settled_todo_count": len(state.settled_todo_ids), - "completion_identity_preserved": bool( - passed and len(state.settled_todo_ids) == expected_count - ), - "terminal_quota_observed": state.terminal_quota_observed, - "model_stopped_after_terminal_quota": bool( - passed and state.terminal_quota_observed - ), - "observed_tool_sequence": [step["kind"] for step in state.steps], - "tool_call_receipts": list(state.steps), - "boundary": { - "raw_prompt_persisted": False, - "raw_provider_response_persisted": False, - "raw_command_persisted": False, + qualification_id=qualification_id, + actor_ref=actor_ref, + passed=passed, + failure_code=failure_code, + steps=state.steps, + extra_fields={ + "scenario": "post_refresh_rejection_reentry", + "provider_call_count": state.provider_call_count, + "projected_todo_count": expected_count, + "settled_todo_count": len(state.settled_todo_ids), + "completion_identity_preserved": bool( + passed and len(state.settled_todo_ids) == expected_count + ), + "terminal_quota_observed": state.terminal_quota_observed, + "model_stopped_after_terminal_quota": bool( + passed and state.terminal_quota_observed + ), + }, + boundary={ "filesystem_writes_executed": True, - "writes_limited_to_temporary_fixture": True, - "external_writes_executed": False, - "shell_commands_executed": False, "read_only_host_commands_executed": True, }, - } + ) def _execute_todo_completion( @@ -1045,45 +997,9 @@ def reject_nested_cli(_argv: list[str], _cwd: Path, _output: str) -> str: return output, kind, workspace_shell -class DoubaoTerminalSettlementToolBehaviorActor: +class DoubaoTerminalSettlementToolBehaviorActor(DoubaoExecToolBehaviorActorBase): """Prove the model follows the real post-spend terminal closeout plan.""" - def __init__( - self, - *, - api_key: str, - model: str = DOUBAO_2_1_PRO_MODEL, - timeout_seconds: float = 90.0, - transport: DoubaoActorTransport = _direct_ark_transport, - ) -> None: - self._client = DoubaoExecToolClient( - api_key=api_key, - model=model, - timeout_seconds=timeout_seconds, - transport=transport, - ) - - @classmethod - def from_environment( - cls, - *, - environ: Mapping[str, str] | None = None, - transport: DoubaoActorTransport = _direct_ark_transport, - timeout_seconds: float = 90.0, - ) -> DoubaoTerminalSettlementToolBehaviorActor: - values = os.environ if environ is None else environ - api_key = values.get(ARK_API_KEY_ENV, "") - if not api_key.strip(): - raise RuntimeError( - "ARK_API_KEY is not injected; live Doubao qualification is unavailable" - ) - return cls( - api_key=api_key, - model=values.get(DOUBAO_MODEL_ENV, DOUBAO_2_1_PRO_MODEL), - timeout_seconds=timeout_seconds, - transport=transport, - ) - def qualify( self, *, @@ -1091,7 +1007,6 @@ def qualify( fixture_root: Path, ) -> dict[str, Any]: fixture = _build_fixture(fixture_root) - digest = sha256(qualification_id.encode("utf-8")).hexdigest()[:16] state = _QualificationState( fixture=fixture, messages=[ @@ -1151,7 +1066,7 @@ def qualify( }, {"role": "user", "content": fixture.task_body}, ], - turn_instance_id=f"qualification-{digest}", + turn_instance_id=qualification_turn_instance_id(qualification_id), ) actor_ref = self._client.actor_ref @@ -1210,20 +1125,7 @@ def receipt(*, passed: bool, failure_code: str | None) -> dict[str, Any]: return receipt(passed=False, failure_code=failure_code) if kind == "terminal_closeout": return receipt(passed=True, failure_code=None) - state.messages.extend( - [ - { - "role": "assistant", - "content": step.assistant_content, - "tool_calls": [tool_call.provider_value], - }, - { - "role": "tool", - "tool_call_id": tool_call.call_id, - "content": output, - }, - ] - ) + append_exec_tool_exchange(state.messages, tool_call, output) return receipt(passed=False, failure_code="tool_call_budget_exhausted") @@ -1438,19 +1340,6 @@ def receipt(*, passed: bool, failure_code: str | None) -> dict[str, Any]: state.steps.append(step_receipt) if failure_code is not None: return receipt(passed=False, failure_code=failure_code) - state.messages.extend( - [ - { - "role": "assistant", - "content": step.assistant_content, - "tool_calls": [tool_call.provider_value], - }, - { - "role": "tool", - "tool_call_id": tool_call.call_id, - "content": output, - }, - ] - ) + append_exec_tool_exchange(state.messages, tool_call, output) return receipt(passed=False, failure_code="tool_call_budget_exhausted")