Skip to content

feat(sdk-python): add get_context_usage() method - #6471

Closed
juhuan wants to merge 1 commit into
QwenLM:mainfrom
juhuan:feat/sdk-get-context-usage
Closed

feat(sdk-python): add get_context_usage() method#6471
juhuan wants to merge 1 commit into
QwenLM:mainfrom
juhuan:feat/sdk-get-context-usage

Conversation

@juhuan

@juhuan juhuan commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Summary

Add get_context_usage(show_details=False) method to the Python SDK's Query class, which sends the get_context_usage control request to the CLI. Also adds the CLIControlGetContextUsageRequest protocol type.

The TS SDK already has getContextUsage(), and the CLI already handles the get_context_usage control request in SystemController.

Test plan

  • Python SDK tests pass (pytest — 58 passed)

Add `get_context_usage(show_details=False)` method to the Python SDK's
Query class, sending the `get_context_usage` control request to the CLI.
Also add `CLIControlGetContextUsageRequest` protocol type.

The TS SDK already has `getContextUsage()`; the CLI already handles
the `get_context_usage` control request in SystemController.

@qwen-code-ci-bot qwen-code-ci-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Hey @juhuan — thanks for the contribution! The code change itself looks clean, but the PR body doesn't follow our PR template. Could you update the description to include the required sections?

Specifically, the template asks for:

  • What this PR does (currently ## Summary)
  • Why it's needed (motivation / problem being solved)
  • Reviewer Test Plan with subsections: How to verify, Evidence (Before & After), Tested on
  • Risk & Scope
  • Linked Issues
  • A <details>中文说明</details> block with a Chinese translation

This helps reviewers (and future-you) understand the context quickly. Happy to re-run triage once it's updated. 🙏

中文说明

@juhuan — 感谢贡献!代码改动本身看起来没问题,但 PR 描述没有按照我们的 PR 模板 填写。能否更新一下描述,补上必要的章节?

具体来说,模板要求包含:

  • What this PR does(目前是 ## Summary
  • Why it's needed(动机 / 要解决的问题)
  • Reviewer Test Plan 及子章节:验证方法、Evidence(Before & After)、Tested on
  • Risk & Scope
  • Linked Issues
  • 一个 <details>中文说明</details> 块,包含中文翻译

这些信息能帮助 reviewer 更快理解上下文。更新后可以重新跑 triage。🙏

Qwen Code · qwen3.7-max

await self._ensure_started()
return await self._send_control_request("mcp_server_status")

async def get_context_usage(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[Suggestion] The async Query class now has get_context_usage(), but SyncQuery in sync_query.py is missing the corresponding sync wrapper. Every other control-request method (supported_commands, mcp_server_status, set_model, set_permission_mode) has a sync counterpart. Sync SDK consumers currently have no way to call this new feature.

Consider adding to SyncQuery (after mcp_server_status):

def get_context_usage(self, show_details: bool = False) -> Any:
    q = self._require_query()
    return asyncio.run_coroutine_threadsafe(
        q.get_context_usage(show_details),
        self._loop,
    ).result(timeout=q.control_request_timeout + _SYNC_TIMEOUT_MARGIN)

— qwen3.7-max via Qwen Code /review

await self._ensure_started()
return await self._send_control_request(
"get_context_usage", {"show_details": show_details}
)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[Suggestion] No tests were added for get_context_usage(). The TS SDK has dedicated unit tests (Query.test.ts lines 1211-1287) covering the request payload, show_details parameter forwarding, response deserialization, and closed-query error handling. The Python SDK has analogous tests for supported_commands() and mcp_server_status() in test_query_core.py and integration tests.

Additionally, the integration test mock in tests/integration/conftest.py has no handler for the get_context_usage subtype — it falls through to the error branch, so integration tests cannot exercise this path.

Suggested additions:

  • Unit test in test_query_core.py verifying the outgoing payload contains {"subtype": "get_context_usage", "show_details": True/False} and that a mock response is returned correctly
  • A get_context_usage branch in conftest.py's mock CLI handler returning a representative response
  • Optionally, an integration test in test_async_query.py

— qwen3.7-max via Qwen Code /review

@wenshao

wenshao commented Jul 7, 2026

Copy link
Copy Markdown
Collaborator

💡 Suggestion: Consolidate SDK PRs

Hi @juhuan, thanks for the comprehensive SDK work! We noticed you have 15 open PRs that all modify the same core files (transport.py, types.py, queryOptionsSchema.ts, types.ts, ProcessTransport.ts, createQuery.ts) and were created on the same day.

The problem

  • Merge conflicts: Since all 15 PRs touch the same files, whichever merges first will cause conflicts in the remaining 14.
  • Review overhead: Reviewing 15 near-identical PRs separately is inefficient and risks fatigue.
  • CI cost: 15 separate CI runs for the same lint/typecheck passes.

Suggestion: regroup into 2 PRs

We recommend closing the current 15 PRs and reopening them as 2 consolidated PRs:

PR 1 — feat(sdk): expose transport and query options in both SDKs

Covers pure SDK-side option additions (~9 current PRs):

PR 2 — feat(sdk): add control request methods to both SDKs

Covers features that also involve CLI-side ControlDispatcher changes (~4 current PRs):

This keeps a reasonable separation of concerns while eliminating the merge-conflict chain and making review much more manageable.

/cc @juhuan

@wenshao

wenshao commented Jul 7, 2026

Copy link
Copy Markdown
Collaborator

Closing in favor of consolidated PRs (see suggestion comment above). Please reopen as 2 grouped PRs.

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.

3 participants