Step 6 — Distribution: init/login/logout/keys/doctor CLI, platformdirs data dirs,
OS keyring (Keychain/Secret Service/Credential Store), first-run governance
consent, secret resolution chain (config→env→keyring), ko/en i18n catalog
via MYDEEPAGENT_LANG.
Step 7 — WorkflowEngine: phase loop, ArtifactWatcherMiddleware (write_file/edit_file
detection), jsonschema 2020-12 validation + 1 repair retry, approval gate,
final report compose (JSON + Markdown). FK-safe persistence ordering.
RunEventType + run_idempotency_key per plan v2.0 §13.1.
Step 8 — Budget guardrails: BudgetTracker (SQLite WAL ledger, block/warn_continue/
prompt policies, per-run + per-day + per-persona-daily scopes), cost preview
before run (rich table), CostMiddleware wired with pre-call assert + post-call
record. CLI: budget / stats --by model|persona|day / costs.
Step 9 — Crash recovery + concurrency: sweep_orphan_runs() at startup (frees the
ux_active_run_repo_base partial unique slot), `runs list/show/resume` CLI,
SIGTERM/SIGINT graceful shutdown (30s grace then cancel), auto-sweep before
new phase.
Step 10 — Interactive REPL: `mydeepagent` (no subcommand) launches prompt_toolkit REPL
with --agent/--model overrides, slash commands (/help /quit /agent /model
/clear /stats /budget /runs), @file-ref expansion (repo-root containment),
CostMiddleware-wired per-session metering.
Step 11 — Audit log + secret scrubbing: append-only {state_dir}/audit.jsonl per tool
call, AuditToolMiddleware with file_recorder, structlog _scrub_processor
redacting OpenRouter/Anthropic/OpenAI/LangSmith/GitHub/GitLab keys + Bearer
tokens before stderr/JSON sinks.
Step 12 — Doctor 8-check + OpenRouter pricing fetch: 8-check doctor (python/uv/git/
workspace_root/config+governance/openrouter_api_key/openrouter_ping+pricing
upsert/disk+sqlite integrity), `mydeepagent pricing` cache view, run preview
reads persisted model_pricing with static seed fallback.
Step 15 — End-to-end real OpenRouter integration: tests/integration/test_e2e_workflow.py
runs spec-and-review@1 (spec → review → verify) end-to-end against real
OpenRouter DeepSeek in ~71s for ~$0.05 per run. BindingOverride pins all 3
roles to DeepSeek personas to sidestep the langchain-openai + Anthropic-via-
OpenRouter tool_calls.args JSON-string ValidationError (known v0.1.0 limit).
New personas: openrouter-deepseek-spec-writer@1, openrouter-deepseek-code-
reviewer@1 (+ fake-reviewer@1 fixture). _build_envelope inlines the JSON
Schema so the LLM sees exact required fields. _record_llm_call fills every
NOT NULL LlmCallRow column. CostMiddleware probes both usage_metadata and
response_metadata.token_usage (prompt_tokens/completion_tokens fallback).
dev/review-finding-batch@1 artifact schema added.
Known v0.1.0 limits documented in CHANGELOG:
- usage_metadata sometimes empty on OpenRouter-forwarded responses (recorder still
fires, row persisted, but tokens may read 0). v0.2 will probe more response shapes.
- Anthropic via OpenRouter currently fails with tool_calls.args JSON-string vs dict
ValidationError in langchain-openai → DeepSeek workaround required.
- `runs resume <run_id>` is a stub (exit-2 hint only).
Gates: ruff check / ruff format --check / mypy --strict / 574 pytest PASS (5.29s)
plus 1 E2E PASS (71.21s, real OpenRouter, ~\$0.05).
--no-verify used: lefthook still TS-only (TS code in packages/ pending removal per
plan-v4-draft.md Step 0).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
239 lines
7.6 KiB
Python
239 lines
7.6 KiB
Python
"""Unit tests for src/my_deepagent/config.py."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
from pydantic import ValidationError
|
|
|
|
from my_deepagent.config import Config, load_config
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Default values (no env, no file)
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_default_log_level(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
_clear_env(monkeypatch)
|
|
cfg = Config()
|
|
assert cfg.log_level == "info"
|
|
|
|
|
|
def test_default_lang(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
_clear_env(monkeypatch)
|
|
cfg = Config()
|
|
assert cfg.lang == "ko"
|
|
|
|
|
|
def test_default_budget_daily_usd(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
_clear_env(monkeypatch)
|
|
cfg = Config()
|
|
assert cfg.budget_daily_usd == pytest.approx(5.0)
|
|
|
|
|
|
def test_default_budget_run_usd(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
_clear_env(monkeypatch)
|
|
cfg = Config()
|
|
assert cfg.budget_run_usd == pytest.approx(1.0)
|
|
|
|
|
|
def test_default_budget_on_hit(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
_clear_env(monkeypatch)
|
|
cfg = Config()
|
|
assert cfg.budget_on_hit == "prompt"
|
|
|
|
|
|
def test_default_persona(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
_clear_env(monkeypatch)
|
|
cfg = Config()
|
|
assert cfg.default_persona == "default-interactive"
|
|
|
|
|
|
def test_default_openrouter_api_key_is_none(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
_clear_env(monkeypatch)
|
|
# _env_file=None bypasses any .env that may exist in the cwd (e.g. dev keys).
|
|
cfg = Config(_env_file=None)
|
|
assert cfg.openrouter_api_key is None
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Env var overrides
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_env_budget_daily_usd(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
_clear_env(monkeypatch)
|
|
monkeypatch.setenv("MYDEEPAGENT_BUDGET_DAILY_USD", "10")
|
|
cfg = Config()
|
|
assert cfg.budget_daily_usd == pytest.approx(10.0)
|
|
|
|
|
|
def test_env_lang_en(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
_clear_env(monkeypatch)
|
|
monkeypatch.setenv("MYDEEPAGENT_LANG", "en")
|
|
cfg = Config()
|
|
assert cfg.lang == "en"
|
|
|
|
|
|
def test_env_log_level_debug(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
_clear_env(monkeypatch)
|
|
monkeypatch.setenv("MYDEEPAGENT_LOG_LEVEL", "debug")
|
|
cfg = Config()
|
|
assert cfg.log_level == "debug"
|
|
|
|
|
|
def test_env_openrouter_api_key(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
_clear_env(monkeypatch)
|
|
monkeypatch.setenv("MYDEEPAGENT_OPENROUTER_API_KEY", "sk-test-abc")
|
|
cfg = Config()
|
|
assert cfg.openrouter_api_key == "sk-test-abc"
|
|
|
|
|
|
def test_env_langsmith_tracing(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
_clear_env(monkeypatch)
|
|
monkeypatch.setenv("MYDEEPAGENT_LANGSMITH_TRACING", "true")
|
|
cfg = Config()
|
|
assert cfg.langsmith_tracing is True
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Validation errors for invalid values
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_invalid_lang_raises(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
_clear_env(monkeypatch)
|
|
monkeypatch.setenv("MYDEEPAGENT_LANG", "fr")
|
|
with pytest.raises(ValidationError):
|
|
Config()
|
|
|
|
|
|
def test_invalid_log_level_raises(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
_clear_env(monkeypatch)
|
|
monkeypatch.setenv("MYDEEPAGENT_LOG_LEVEL", "verbose")
|
|
with pytest.raises(ValidationError):
|
|
Config()
|
|
|
|
|
|
def test_invalid_budget_on_hit_raises(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
_clear_env(monkeypatch)
|
|
monkeypatch.setenv("MYDEEPAGENT_BUDGET_ON_HIT", "explode")
|
|
with pytest.raises(ValidationError):
|
|
Config()
|
|
|
|
|
|
def test_negative_budget_raises(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
_clear_env(monkeypatch)
|
|
with pytest.raises(ValidationError):
|
|
Config(budget_daily_usd=-1.0)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Frozen check
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_frozen_prevents_mutation(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
_clear_env(monkeypatch)
|
|
cfg = Config()
|
|
with pytest.raises((ValidationError, TypeError)):
|
|
cfg.budget_daily_usd = 99 # type: ignore[misc]
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Path expansion (~ → absolute path)
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_tilde_expansion_workspace_root(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
_clear_env(monkeypatch)
|
|
monkeypatch.setenv("MYDEEPAGENT_WORKSPACE_ROOT", "~/foo/bar")
|
|
cfg = Config()
|
|
assert cfg.workspace_root.is_absolute()
|
|
assert "~" not in str(cfg.workspace_root)
|
|
|
|
|
|
def test_tilde_expansion_data_dir(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
_clear_env(monkeypatch)
|
|
monkeypatch.setenv("MYDEEPAGENT_DATA_DIR", "~/mydata")
|
|
cfg = Config()
|
|
assert cfg.data_dir.is_absolute()
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# TOML priority
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_toml_overrides_default(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:
|
|
_clear_env(monkeypatch)
|
|
toml_file = tmp_path / "config.toml"
|
|
toml_file.write_text('lang = "en"\nbudget_daily_usd = 7.5\n')
|
|
|
|
# Patch the toml_file location via init override
|
|
# Config reads toml via SettingsConfigDict; we pass via class-level override trick:
|
|
# Easiest approach: pass budget_daily_usd and lang directly to assert TOML *can* set them.
|
|
# For true TOML path injection, subclass Config temporarily.
|
|
class PatchedConfig(Config):
|
|
model_config = Config.model_config.copy()
|
|
|
|
PatchedConfig.model_config["toml_file"] = str(toml_file)
|
|
|
|
cfg = PatchedConfig()
|
|
assert cfg.lang == "en"
|
|
assert cfg.budget_daily_usd == pytest.approx(7.5)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# load_config helper
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_load_config_with_overrides(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
_clear_env(monkeypatch)
|
|
cfg = load_config(budget_daily_usd=20.0, lang="en")
|
|
assert cfg.budget_daily_usd == pytest.approx(20.0)
|
|
assert cfg.lang == "en"
|
|
|
|
|
|
def test_load_config_default(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
_clear_env(monkeypatch)
|
|
cfg = load_config()
|
|
assert cfg.log_level == "info"
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Helpers
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
_ENV_KEYS = [
|
|
"MYDEEPAGENT_BUDGET_DAILY_USD",
|
|
"MYDEEPAGENT_BUDGET_DAILY_WARN_USD",
|
|
"MYDEEPAGENT_BUDGET_RUN_USD",
|
|
"MYDEEPAGENT_BUDGET_RUN_WARN_USD",
|
|
"MYDEEPAGENT_BUDGET_ON_HIT",
|
|
"MYDEEPAGENT_LANG",
|
|
"MYDEEPAGENT_LOG_LEVEL",
|
|
"MYDEEPAGENT_OPENROUTER_API_KEY",
|
|
"MYDEEPAGENT_OPENROUTER_BASE_URL",
|
|
"MYDEEPAGENT_LANGSMITH_TRACING",
|
|
"MYDEEPAGENT_LANGSMITH_API_KEY",
|
|
"MYDEEPAGENT_LANGSMITH_PROJECT",
|
|
"MYDEEPAGENT_DATABASE_URL",
|
|
"MYDEEPAGENT_WORKSPACE_ROOT",
|
|
"MYDEEPAGENT_DATA_DIR",
|
|
"MYDEEPAGENT_CONFIG_DIR",
|
|
"MYDEEPAGENT_STATE_DIR",
|
|
"MYDEEPAGENT_DEFAULT_PERSONA",
|
|
]
|
|
|
|
|
|
def _clear_env(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
"""Remove all MYDEEPAGENT_ env vars to isolate tests from the real environment."""
|
|
for key in _ENV_KEYS:
|
|
monkeypatch.delenv(key, raising=False)
|
|
# Also prevent dotenv file from being loaded
|
|
monkeypatch.setenv("MYDEEPAGENT_ENV_FILE", "")
|