Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions core/llm_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
def set_context_provider(
provider: Optional["LLMProvider"],
) -> Token[Optional["LLMProvider"]]:
"""Set the provider for the current context (request). Returns a Token
that MUST be passed to reset_context_provider when the request ends -
see api/dependencies for the yield-dependency that does this."""
"""Set the provider for the current context (request).

Returns a Token that MUST be passed to reset_context_provider()
Expand All @@ -48,6 +51,11 @@ def set_context_provider(


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`.

"""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
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."""
"""Undo a prior set_context_provider call.

Always call this in a `finally` block paired with the
Expand Down
Loading