Skip to content

fix: type the ContextVar BYOK Token (fixes #30) - #50

Closed
sgoel2be24-cyber wants to merge 2 commits into
DevSidd2006:mainfrom
sgoel2be24-cyber:fix/contextvar-typed-token
Closed

sgoel2be24-cyber wants to merge 2 commits into
DevSidd2006:mainfrom
sgoel2be24-cyber:fix/contextvar-typed-token

Conversation

@sgoel2be24-cyber

@sgoel2be24-cyber sgoel2be24-cyber commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #30

What changed

set_context_provider/reset_context_provider now use Token[Optional[LLMProvider]] — a Token from a different ContextVar is a type error instead of a silent cross-context reset. Lifecycle docs clarified (set/reset paired in a finally; the provider never outlives its request).

Testing

Round-trip verified (set → reset → get returns None); no behavior change — typing + docs only.

Summary by Sourcery

Type context-provider tokens to their associated ContextVar and clarify safe request-scoped cleanup.

Bug Fixes:

  • Prevent cross-context provider resets by enforcing the correct ContextVar token type.

Enhancements:

  • Clarify the provider lifecycle and require paired cleanup in a finally block so providers do not outlive their requests.

set_context_provider/reset_context_provider now use the typed
Token[Optional[LLMProvider]] — a Token from a different ContextVar is a
type error instead of a silent cross-context reset. Lifecycle docs
clarified (pair set/reset in a finally; the provider never outlives its
request).
@sourcery-ai

sourcery-ai Bot commented Oct 10, 2026 •

Copy link
Copy Markdown
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

The PR updates the context provider helpers to use Token[Optional[LLMProvider]], allowing type checkers to reject tokens from unrelated ContextVars without changing runtime behavior, and clarifies safe request-lifecycle usage in the API documentation strings.

Sequence diagram for typed context provider lifecycle

sequenceDiagram
    participant Request
    participant ContextHelpers
    participant ContextVar

    Request->>ContextHelpers: set_context_provider(provider)
    ContextHelpers->>ContextVar: set(provider)
    ContextVar-->>ContextHelpers: Token[Optional[LLMProvider]]
    ContextHelpers-->>Request: token
    Request->>ContextHelpers: reset_context_provider(token)
    ContextHelpers->>ContextVar: reset(token)
    ContextVar-->>Request: provider context restored
Loading

File-Level Changes

Change Details Files
Strengthen ContextVar token typing to prevent cross-context misuse while clarifying lifecycle requirements.
  • Return a value-typed token from the context provider setter.
  • Require the matching value-typed token when resetting the provider.
  • Document pairing set/reset in a finally block and request-scoped provider lifetime.
core/llm_context.py

Assessment against linked issues

Issue Objective Addressed Explanation
#30 Use a typed Token[Optional[LLMProvider]] return and parameter for the ContextVar provider helpers, preventing tokens from unrelated ContextVars from being accepted silently. ✅
#30 Clarify the ContextVar provider lifecycle documentation, including pairing set/reset in a finally block and ensuring the provider does not outlive its request. ✅

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@github-actions

Copy link
Copy Markdown

👋 Thanks for opening this PR! A maintainer will review it soon.

In the meantime, please make sure you've read CONTRIBUTING.md and filled out the PR template, including screenshots for any visual changes.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hey - I've found 2 issues

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="core/llm_context.py" line_range="53" />
<code_context>
+    type error, not a silent cross-context reset. Always call this in a
     `finally` block paired with the `set_context_provider` that produced
     `token`, so a provider never outlives the request that set it."""
     _current_llm_provider.reset(token)
</code_context>
<issue_to_address>
**Child tasks retain request credentials**

When a request creates a child task while the provider is set and that task continues after the request ends, `asyncio.create_task` copies the current context, so `reset_context_provider` clears only the request context and the child retains the BYOK provider, allowing it to use that credential after the request ends.

Prevent spawned tasks from retaining or using the request provider after the request ends, for example by preventing provider propagation or cancelling and draining those tasks.

Also at `core/llm_context.py:47`, `core/llm_context.py:51-52`.
</issue_to_address>

### Comment 2
<location path="core/llm_context.py" line_range="47" />
<code_context>

-def reset_context_provider(token: Token) -> None:
-    """Undo a prior set_context_provider call. Always call this in a
+def reset_context_provider(token: Token[Optional["LLMProvider"]]) -> None:
+    """Undo a prior set_context_provider call. The Token is typed to this
+    ContextVar's value type, so a Token from a different ContextVar is a
</code_context>
<issue_to_address>
**Cross-context cleanup fails at runtime**

When a caller passes a token from another `ContextVar[Optional[LLMProvider]]`, `Token[Optional[LLMProvider]]` checks only the value type, so `_current_llm_provider.reset(token)` raises `ValueError` instead of rejecting the mix-up statically, and cleanup fails at runtime.

Remove the claim that the annotation catches cross-ContextVar tokens, or check `token.var` against `_current_llm_provider` before resetting.

Also at `core/llm_context.py:48-50`.
</issue_to_address>

Sourcery assessment

Approval pending. 2 findings to address first.

Blocking findings: core/llm_context.py:53, core/llm_context.py:47


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread core/llm_context.py
type error, not a silent cross-context reset. Always call this in a
`finally` block paired with the `set_context_provider` that produced
`token`, so a provider never outlives the request that set it."""
_current_llm_provider.reset(token)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Medium · Child tasks retain request credentials

When a request creates a child task while the provider is set and that task continues after the request ends, asyncio.create_task copies the current context, so reset_context_provider clears only the request context and the child retains the BYOK provider, allowing it to use that credential after the request ends.

Prevent spawned tasks from retaining or using the request provider after the request ends, for example by preventing provider propagation or cancelling and draining those tasks.

Also at core/llm_context.py:47, core/llm_context.py:51-52.

Prompt for AI agents
In `core/llm_context.py` at line 53:

**Child tasks retain request credentials**

When a request creates a child task while the provider is set and that task continues after the request ends, `asyncio.create_task` copies the current context, so `reset_context_provider` clears only the request context and the child retains the BYOK provider, allowing it to use that credential after the request ends.

Prevent spawned tasks from retaining or using the request provider after the request ends, for example by preventing provider propagation or cancelling and draining those tasks.

Also at `core/llm_context.py:47`, `core/llm_context.py:51-52`.

Comment thread core/llm_context.py

def reset_context_provider(token: Token) -> None:
"""Undo a prior set_context_provider call. Always call this in a
def reset_context_provider(token: Token[Optional["LLMProvider"]]) -> None:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Medium · Cross-context cleanup fails at runtime

When a caller passes a token from another ContextVar[Optional[LLMProvider]], Token[Optional[LLMProvider]] checks only the value type, so _current_llm_provider.reset(token) raises ValueError instead of rejecting the mix-up statically, and cleanup fails at runtime.

Remove the claim that the annotation catches cross-ContextVar tokens, or check token.var against _current_llm_provider before resetting.

Also at core/llm_context.py:48-50.

Prompt for AI agents
In `core/llm_context.py` at line 47:

**Cross-context cleanup fails at runtime**

When a caller passes a token from another `ContextVar[Optional[LLMProvider]]`, `Token[Optional[LLMProvider]]` checks only the value type, so `_current_llm_provider.reset(token)` raises `ValueError` instead of rejecting the mix-up statically, and cleanup fails at runtime.

Remove the claim that the annotation catches cross-ContextVar tokens, or check `token.var` against `_current_llm_provider` before resetting.

Also at `core/llm_context.py:48-50`.

@DevSidd2006 DevSidd2006 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Two changes to fix:

  1. When a caller passes a token from another ContextVar[Optional[LLMProvider]], Token[Optional[LLMProvider]] checks only the value type, so _current_llm_provider.reset(token) raises ValueError instead of rejecting the mix-up statically, and cleanup fails at runtime. Remove the claim that the annotation catches cross-ContextVar tokens, or check token.var against _current_llm_provider before resetting.

  2. When a request creates a child task while the provider is set and that task continues after the request ends, asyncio.create_task copies the current context, so reset_context_provider clears only the request context and the child retains the BYOK provider, allowing it to use that credential after the request ends. Prevent spawned tasks from retaining or using the request provider after the request ends, for example by preventing provider propagation or cancelling and draining those tasks. Also at core/llm_context.py:47, core/llm_context.py:51-52

@DevSidd2006
DevSidd2006 self-requested a review October 28, 2026 15:34

@DevSidd2006 DevSidd2006 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Closing as this is duplicate PR of #31

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.

fix: improve ContextVar provider typing

2 participants