Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/agent_env/task/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,21 @@

logger = logging.getLogger(__name__)


def failure_text(error: BaseException) -> str:
"""A run's recorded error: ``str(error)``, or its class (and its cause's) when that is empty.

``TimeoutError()``, ``httpx.ConnectError('')`` and others have no message, and a failed run
recorded with ``str()`` of one says nothing about why it failed."""
text = str(error)
if text:
return text
name = type(error).__name__
cause = error.__cause__ or (None if error.__suppress_context__ else error.__context__)
if cause is None:
return name
return f"{name} (from {type(cause).__name__}: {cause})" if str(cause) else f"{name} (from {type(cause).__name__})"

TASKS_COLLECTION = "tasks"
TASK_INSTANCES_COLLECTION = "task_instances"
TASK_STEP_JOURNAL_COLLECTION = "task_step_journal"
Expand Down Expand Up @@ -760,7 +775,7 @@ def record_task_failure_sync(
spec = UpdateSpec(
set={
"status": "failed",
"error": str(error),
"error": failure_text(error),
"context": context.to_safe_dict(),
"completed_at_utc": completed_at_utc,
},
Expand Down
43 changes: 43 additions & 0 deletions tst/unit/task/task_failure_write_retry_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,46 @@ def unreachable(*args, **kwargs):
monkeypatch.setattr(TaskInstanceStore, "upsert_instance", unreachable)
with pytest.raises(ConnectionError):
register_task_instance("t", 1, 1, instance_id="i")


def test_failure_text_keeps_a_message_and_names_a_messageless_error_and_its_cause():
assert store_mod.failure_text(RuntimeError("the model refused this task")) == "the model refused this task"
assert store_mod.failure_text(TimeoutError()) == "TimeoutError"
try:
try:
raise ConnectionResetError("peer reset")
except ConnectionResetError as e:
raise TimeoutError() from e
except TimeoutError as chained:
assert store_mod.failure_text(chained) == "TimeoutError (from ConnectionResetError: peer reset)"
try:
try:
raise KeyError("")
except KeyError:
raise EOFError()
except EOFError as implicit:
assert store_mod.failure_text(implicit) == "EOFError (from KeyError: '')"
try:
try:
raise OSError()
except OSError:
raise EOFError() from None
except EOFError as suppressed:
assert store_mod.failure_text(suppressed) == "EOFError"


@pytest.mark.asyncio
async def test_a_run_failed_by_a_messageless_error_records_its_class(tmp_path, monkeypatch):
monkeypatch.setattr(store_mod, "_FAILURE_WRITE_BACKOFF_SECONDS", 0)
store = _sqlite_store(tmp_path)
store.upsert_instance(instance_id="inst-1", task_id="t", task_version=1, total_steps=5)
set_task_instance_store(store)
try:
await record_task_failure(
instance_id="inst-1", failing_step_id="prompt_agent", context=TaskStepContext(),
error=TimeoutError(), completed_at_utc="2026-11-12 19:00 UTC",
)
inst = store.get("inst-1")
finally:
set_task_instance_store(None)
assert (inst.status, inst.error) == ("failed", "TimeoutError")
Loading