Skip to content

test: unit tests for response_api + tracking (99% coverage, 5 xfail'd bugs) - #60

Open
devin-ai-integration[bot] wants to merge 1 commit into
mainfrom
devin/unit-tests-response-tracking
Open

devin-ai-integration[bot] wants to merge 1 commit into
mainfrom
devin/unit-tests-response-tracking

Conversation

@devin-ai-integration

@devin-ai-integration devin-ai-integration Bot commented Oct 14, 2026 •

Copy link
Copy Markdown

Summary

Adds hermetic unit tests for agentlab.llm.response_api and agentlab.llm.tracking. Two new files only, no src/ changes. Child 3/11 of the unit-test fan-out.

  • tests/llm/test_response_api_unit.py: ToolCall/ToolCalls, the MessageBuilder base (to_markdown with tool calls, add_image_url, add_responded_tool_calls), and all three provider builders (prepare_message, handle_tool_call, content transforms, cache markers). Also APIPayload validation, tool_call_to_python_code, AgentlabAction, and the _call_api param mapping (tool_choice / force_call_tool / cache flags) plus _parse_response for OpenAIResponseModel, OpenAIChatCompletionModel and ClaudeResponseModel, and every *ModelArgs factory. The call_*_api_with_retries helpers are patched, so no HTTP calls are made.
  • tests/llm/test_tracking_unit.py: LLMTracker; set_tracker nesting and restore-on-exception, plus a two-thread Barrier test showing thread-local trackers don't leak into each other; cost_tracker_decorator. All four get_pricing_* functions are covered: requests.get is mocked for OpenRouter, and the langchain/litellm tables are faked. Also covers _remove_version_suffix, the whole TrackAPIPricingMixin (pricing fallback chain, token extraction, __call__ stats), and table tests of effective cost: Anthropic cache read/write and OpenAI chat prompt_tokens_details vs responses input_tokens_details, using exact numbers and real SDK usage objects. The module-level langchain import fallbacks are covered by running a copy of the module.

Coverage (target modules)

module before (whole suite on main) after (new files only)
agentlab.llm.response_api 70% 99%
agentlab.llm.tracking 70% 99%

Lines still uncovered: response_api.py 426, 431 (pass bodies of abstract methods) and tracking.py 378-379 (an else that can't be reached because api_type is always "chatcompletion" or "response").

Tests: 169 collected (164 pass + 5 strict xfail). Both files together take about 5s. They pass serially, with -n 4, and in random order (pytest-randomly, several seeds, together with the existing tests/llm).

Source bugs found (tests are xfail(strict=True); src/ not modified)

  1. get_effective_cost_from_antrophic_api reads usage.cache_input_tokens, but Anthropic names this field cache_read_input_tokens, so cache-read tokens are never billed.
  2. On the same path, an anthropic.types.Usage leaves cache_creation_input_tokens/cache_read_input_tokens as None when caching isn't used. None * float then raises TypeError where the cost should be 0.
  3. OpenAIChatCompletionModel._extract_thinking_content_from_response: tool-call-only responses have content=None, and it ends up as the literal string "None" in LLMOutput.think.
  4. ClaudeResponseModel.filter_system_messages checks c.get("type") == "image", but builder content looks like {"image": ...}, so an image added to a system message with add_image is never rejected.
  5. OpenAIChatCompletionModel.__init__ doesn't pass api_key to super(), so model.api_key is None even when a key was given (the client still gets it).

Run

uv run pytest tests/llm/test_response_api_unit.py tests/llm/test_tracking_unit.py -p no:cacheprovider -q        # add -n 4 for xdist
uv run --with pytest-cov pytest tests/llm/test_response_api_unit.py tests/llm/test_tracking_unit.py -p no:cacheprovider -q --cov=agentlab.llm.response_api --cov=agentlab.llm.tracking --cov-report=term-missing

Devin-Org: engineering

Link to Devin session: https://devin-gtm.devinenterprise.com/sessions/3d7d885106884ebe8bca1efe65c5bfa4
Open in Devin Desktop: https://devin-gtm.devinenterprise.com/desktop/session/3d7d885106884ebe8bca1efe65c5bfa4?variant=devin
Requested by: @danigrim


Devin Review

Status Commit
⚪ Not started —

Run Devin Review

Devin Review (Staging)


Devin Review

…cost logic

Co-Authored-By: Daniella Grimberg <daniella@cognition.ai>
@devin-ai-integration

Copy link
Copy Markdown
Author

I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".

  • Disable automatic comment, CI, and merge conflict monitoring

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Devin Review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant