Python rewrite of the agent harness on top of deepagents 0.6.1 + langchain 1.x, replacing the abandoned TS attempt in packages/. 388 unit/integration tests pass. Steps ----- 0. Scaffolding — uv workspace, ruff/mypy/pre-commit/alembic, src/tests/docs trees with docs/schemas/ seeded from my-deepagent-seed/. 1. Core — config (pydantic-settings with MYDEEPAGENT_ env prefix and TOML source), enums (Backend, Capability, RiskLevel, ApprovalDecisionAction, ApprovalState, RunState, RunPhaseState, SessionState, ErrorClass), errors (MyDeepAgentError + BudgetExhaustedError with PEP-3134 cause + context suppression), hash (canonical JSON + sha256). 2. Persona/Workflow/Binding — pydantic v2 schemas with tuple-based deep immutability (post-construction hash drift prevented), YAML loaders, deterministic auto-select (preferred_backends → version → name → hash), override resolution with ineligibility diagnostics, PersonaConsentStore with fcntl.flock + tmp+fsync+rename atomic write. 3. Artifact schema registry — Draft202012Validator, multi-root resolution, structured ValidationFinding output. 4. Persistence — 18 SQLAlchemy 2.0 async ORM models with FK CASCADE/RESTRICT, WAL + busy_timeout + foreign_keys PRAGMA, alembic baseline + ux_active_run_repo_base partial unique index, LangGraph SqliteSaver as context manager only (lifecycle safety). 5. DeepAgent session — build_agent wires Persona → create_deep_agent with LocalShellBackend / FilesystemBackend / StateBackend / CompositeBackend, ChatOpenAI(base_url=openrouter) for openrouter: model strings, and 4 middleware classes (cost / audit-tool / safety-shell / fallback-model). Critical workarounds -------------------- - deepagents 0.6.1 rejects FilesystemPermission together with backends that implement SandboxBackendProtocol (LocalShellBackend). SafetyShellMiddleware enforces destructive-command and secret-path policy at the tool layer instead, and build_agent strips the permissions kwarg when the persona's deepagents_backend is local_shell. - FilesystemOperation in deepagents is Literal['read', 'write'] only; _map_operations collapses our richer schema (read/write/edit/ls) safely. Real OpenRouter smoke --------------------- test_openrouter_deepagents_local_shell_smoke calls DeepSeek via deepagents + LocalShellBackend + SafetyShellMiddleware end-to-end. PASS, ~$0.000001 cost, input=9 / output=1 tokens with content "OK". Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
209 lines
6.5 KiB
Python
209 lines
6.5 KiB
Python
"""Unit tests for src/my_deepagent/errors.py."""
|
|
|
|
from uuid import UUID, uuid4
|
|
|
|
import pytest
|
|
|
|
from my_deepagent.enums import ErrorClass
|
|
from my_deepagent.errors import BudgetExhaustedError, MyDeepAgentError
|
|
|
|
|
|
def test_cause_sets_suppress_context() -> None:
|
|
"""Wrapping a cause must suppress the implicit context per PEP 3134."""
|
|
original = ValueError("root cause")
|
|
err = MyDeepAgentError.recoverable("wrapped", cause=original)
|
|
assert err.__cause__ is original
|
|
assert err.__suppress_context__ is True
|
|
|
|
|
|
def test_no_cause_does_not_set_suppress_context() -> None:
|
|
err = MyDeepAgentError.recoverable("no_cause")
|
|
assert err.__cause__ is None
|
|
assert err.__suppress_context__ is False
|
|
|
|
|
|
def test_factory_returns_base_class_not_subclass() -> None:
|
|
"""LSP fix: factory methods always return MyDeepAgentError, never BudgetExhaustedError."""
|
|
err = BudgetExhaustedError.recoverable("foo")
|
|
assert type(err) is MyDeepAgentError
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# MyDeepAgentError factory methods
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_recoverable_class() -> None:
|
|
err = MyDeepAgentError.recoverable("network_blip", recovery_hint="retry")
|
|
assert err.error_class == ErrorClass.RECOVERABLE
|
|
|
|
|
|
def test_recoverable_code() -> None:
|
|
err = MyDeepAgentError.recoverable("network_blip")
|
|
assert err.code == "network_blip"
|
|
|
|
|
|
def test_recoverable_recovery_hint() -> None:
|
|
err = MyDeepAgentError.recoverable("network_blip", recovery_hint="retry after 1s")
|
|
assert err.recovery_hint == "retry after 1s"
|
|
|
|
|
|
def test_human_required_class() -> None:
|
|
err = MyDeepAgentError.human_required("destructive_command_blocked")
|
|
assert err.error_class == ErrorClass.HUMAN_REQUIRED
|
|
|
|
|
|
def test_human_required_code() -> None:
|
|
err = MyDeepAgentError.human_required("destructive_command_blocked")
|
|
assert err.code == "destructive_command_blocked"
|
|
|
|
|
|
def test_fatal_class() -> None:
|
|
err = MyDeepAgentError.fatal("unrecoverable_state")
|
|
assert err.error_class == ErrorClass.FATAL
|
|
|
|
|
|
def test_fatal_code() -> None:
|
|
err = MyDeepAgentError.fatal("unrecoverable_state")
|
|
assert err.code == "unrecoverable_state"
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# run_id / phase_id context
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_run_id_attached() -> None:
|
|
run_id = uuid4()
|
|
err = MyDeepAgentError.recoverable("timeout", run_id=run_id)
|
|
assert err.run_id == run_id
|
|
|
|
|
|
def test_phase_id_attached() -> None:
|
|
phase_id = uuid4()
|
|
err = MyDeepAgentError.recoverable("artifact_missing", phase_id=phase_id)
|
|
assert err.phase_id == phase_id
|
|
|
|
|
|
def test_run_id_none_by_default() -> None:
|
|
err = MyDeepAgentError.recoverable("x")
|
|
assert err.run_id is None
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# __cause__ propagation
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_cause_propagation() -> None:
|
|
original = ValueError("root cause")
|
|
err = MyDeepAgentError.recoverable("wrapped", cause=original)
|
|
assert err.__cause__ is original
|
|
|
|
|
|
def test_cause_none_by_default() -> None:
|
|
err = MyDeepAgentError.recoverable("no_cause")
|
|
assert err.__cause__ is None
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# __repr__ format
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_repr_contains_class_and_code() -> None:
|
|
err = MyDeepAgentError.recoverable("some_code")
|
|
r = repr(err)
|
|
assert "class=recoverable" in r
|
|
assert "code=some_code" in r
|
|
|
|
|
|
def test_repr_contains_run_id_when_present() -> None:
|
|
run_id = UUID("12345678-1234-5678-1234-567812345678")
|
|
err = MyDeepAgentError.recoverable("x", run_id=run_id)
|
|
assert str(run_id) in repr(err)
|
|
|
|
|
|
def test_repr_contains_hint_when_present() -> None:
|
|
err = MyDeepAgentError.recoverable("x", recovery_hint="do something")
|
|
assert "do something" in repr(err)
|
|
|
|
|
|
def test_repr_no_hint_when_absent() -> None:
|
|
err = MyDeepAgentError.recoverable("x")
|
|
assert "hint" not in repr(err)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Exception hierarchy
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_my_deepagent_error_is_exception() -> None:
|
|
err = MyDeepAgentError.recoverable("x")
|
|
assert isinstance(err, Exception)
|
|
|
|
|
|
def test_budget_exhausted_is_my_deepagent_error() -> None:
|
|
err = BudgetExhaustedError("day:2026-05-15", 1.20, 1.00)
|
|
assert isinstance(err, MyDeepAgentError)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# BudgetExhaustedError
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_budget_exhausted_scope() -> None:
|
|
err = BudgetExhaustedError("day:2026-05-15", 1.20, 1.00)
|
|
assert err.scope == "day:2026-05-15"
|
|
|
|
|
|
def test_budget_exhausted_projected_usd() -> None:
|
|
err = BudgetExhaustedError("day:2026-05-15", 1.20, 1.00)
|
|
assert err.projected_usd == pytest.approx(1.20)
|
|
|
|
|
|
def test_budget_exhausted_cap_usd() -> None:
|
|
err = BudgetExhaustedError("day:2026-05-15", 1.20, 1.00)
|
|
assert err.cap_usd == pytest.approx(1.00)
|
|
|
|
|
|
def test_budget_exhausted_error_class() -> None:
|
|
err = BudgetExhaustedError("day:2026-05-15", 1.20, 1.00)
|
|
assert err.error_class == ErrorClass.HUMAN_REQUIRED
|
|
|
|
|
|
def test_budget_exhausted_code() -> None:
|
|
err = BudgetExhaustedError("day:2026-05-15", 1.20, 1.00)
|
|
assert err.code == "budget_exhausted"
|
|
|
|
|
|
def test_budget_exhausted_default_recovery_hint() -> None:
|
|
err = BudgetExhaustedError("day:2026-05-15", 1.20, 1.00)
|
|
assert err.recovery_hint is not None
|
|
assert len(err.recovery_hint) > 0
|
|
|
|
|
|
def test_budget_exhausted_custom_recovery_hint() -> None:
|
|
err = BudgetExhaustedError("day:2026-05-15", 1.20, 1.00, recovery_hint="call support")
|
|
assert err.recovery_hint == "call support"
|
|
|
|
|
|
def test_budget_exhausted_run_id() -> None:
|
|
run_id = uuid4()
|
|
err = BudgetExhaustedError("run:abc", 0.5, 0.4, run_id=run_id)
|
|
assert err.run_id == run_id
|
|
|
|
|
|
def test_budget_exhausted_message_contains_scope() -> None:
|
|
err = BudgetExhaustedError("day:2026-05-15", 1.20, 1.00)
|
|
assert "day:2026-05-15" in str(err)
|
|
|
|
|
|
def test_budget_exhausted_message_contains_values() -> None:
|
|
err = BudgetExhaustedError("scope", 1.2345, 1.0000)
|
|
msg = str(err)
|
|
assert "1.2345" in msg
|
|
assert "1.0000" in msg
|