Skip to content

fix: align AgentSideConnection.to_agent type with AgentFactory - #157

Open
extreme-structure wants to merge 1 commit into
agentclientprotocol:mainfrom
extreme-structure:fix/agent-factory-type-alignment
Open

extreme-structure wants to merge 1 commit into
agentclientprotocol:mainfrom
extreme-structure:fix/agent-factory-type-alignment

Conversation

@extreme-structure

Copy link
Copy Markdown

Problem

AgentSideConnection declares its factory parameter as Callable[[Client], Agent] | Agent, but _prepare always invokes it with the AgentSideConnection itself (to_agent(self)). The narrower AgentFactory = Callable[[AgentSideConnection], Agent] used by AcpServer is therefore not assignable to it under parameter contravariance.

Result: a strict type-checker consumer (mypy --strict / ty) that passes an AgentFactory into AgentSideConnection gets a spurious error at the call site — e.g. AcpServer._handle_initialize's AgentSideConnection(self.agent_factory, transport) — and has to insert a cast.

Fix

Narrow the four to_agent declarations to Callable[[AgentSideConnection], Agent] | Agent so they match AgentFactory:

  • AgentSideConnection.__init__
  • AgentSideConnection.attach
  • AgentSideConnection._prepare
  • spawn_client_process

Also drop the now-unused Client import from agent/connection.py.

Notes

  • The symmetric client side (ClientSideConnection.__init__ / spawn_agent_process) uses to_client: Callable[[Agent], Client] | Client where the factory is actually invoked with the ClientSideConnection itself — a similar but currently self-consistent imprecision, left out of scope here.
  • I wasn't able to run ty check in my environment (dependency fetch timed out); the change is annotation-only and py_compile passes.

AgentSideConnection declares its factory parameter as
Callable[[Client], Agent] | Agent, but _prepare always invokes it with the
AgentSideConnection itself (to_agent(self)). The narrower
AgentFactory = Callable[[AgentSideConnection], Agent] used by AcpServer is
therefore not assignable to it under parameter contravariance, so a strict
type-checker consumer passing an AgentFactory into AgentSideConnection gets a
spurious type error (and has to cast).

Narrow the four to_agent declarations to
Callable[[AgentSideConnection], Agent] | Agent so they match AgentFactory, and
drop the now-unused Client import.

This branch has not been deployed

No deployments
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