Skip to content

API Reference parrot_tools.lsp.session

github-actions[bot] edited this page Dec 2, 2026 · 4 revisions

parrot_tools.lsp.session

View source

Own Pyright startup, requests and bounded process shutdown (FEAT-580, M2).

:class:PyrightSession owns exactly one Pyright pyright-langserver child process for exactly one (config, generation) pair. It composes the byte-level framing in :mod:parrot_tools.lsp.protocol under a single child-process owner: it verifies the server's version, spawns the process with an explicit working directory/root, negotiates capabilities and configuration during the initialize/initialized handshake, continuously drains stdout/stderr so the child never blocks on a full pipe, maps outgoing request ids to :class:asyncio.Future objects, answers server-to-client requests (and explicitly rejects workspace/applyEdit), and tears the process down through a bounded shutdown/exit -> terminate -> kill -> reap sequence.

:meth:PyrightSession.sync_documents and :meth:PyrightSession.diagnostics additionally own exact on-disk document synchronization (didOpen/ didChange/didClose, monotonic versions, a 20-document cap) and versioned push-diagnostic collection (freshness matched against the currently open document version, explicit missing/unversioned coverage, a 2,000 raw diagnostic cap, and warm per-path reuse for unchanged source states). Nothing here interprets or crops diagnostic messages for display, computes workspace digests, or builds baseline deltas — that is the toolkit's job (a later task).

Class PyrightSession

Owns one Pyright child process: startup, JSON-RPC requests, shutdown.

Each instance owns exactly one process for exactly one (config, generation) pair: call :meth:start once, issue zero or more :meth:request calls, then always call :meth:close — a session that failed to start or has been closed is never restarted, and a fresh PyrightSession must be constructed for the next generation.

Parameter Type Default Description
shutdown_timeout_s float _DEFAULT_SHUTDOWN_TIMEOUT_S Bound on waiting for a shutdown response during :meth:close before proceeding straight to terminate.
terminate_timeout_s float _DEFAULT_TERMINATE_TIMEOUT_S Bound on waiting for the process to exit after SIGTERM before escalating to SIGKILL.
kill_timeout_s float _DEFAULT_KILL_TIMEOUT_S Bound on waiting for the process to exit (be reaped) after SIGKILL.

Attributes:

Attribute Type Description
generation int | None

Method PyrightSession.start

Start the owned Pyright process for one (config, generation) pair.

Verifies config.version_command reports exactly config.expected_server_version, spawns config.server_command with an explicit cwd/root and a Node heap option, begins continuously draining stdout/stderr, then performs the initialize/initialized handshake (capabilities and workspace/configuration negotiation). Any failure at any stage cleans up whatever was partially started (process killed, tasks cancelled, pending futures failed) before raising.

start(config: LSPConfig, generation: int) -> None

Parameter Type Default Description
config LSPConfig Trusted, immutable, already-validated configuration. Never mutated by this session.
generation int The caller's monotonically increasing generation identifier for this process instance; stored verbatim on :attr:generation, never generated internally.

Raises:

  • LSPFailure — "server_missing" if either command's executable cannot be found or the version command exits non-zero; "server_version_mismatch" if the reported version does not match config.expected_server_version; "startup_timeout" if the version check or the handshake does not complete within config.startup_timeout_s; "server_crashed" if the process exits/EOFs during the handshake; "protocol_error" if initialize itself returns a JSON-RPC error; "invalid_request" if this session has already been started.

Method PyrightSession.request

Issue one client-initiated JSON-RPC request and await its response.

request(method: str, params: dict[str, Any], timeout_s: float) -> Any

Parameter Type Default Description
method str The LSP method to call. Must be one of the fixed, allowlisted methods this session permits — arbitrary methods and workspace/executeCommand are always rejected, never forwarded to the server.
params dict[str, Any] The request's JSON-RPC params object.
timeout_s float Bound on waiting for the response.

Returns: Any — The response's result value.

Raises:

  • LSPFailure — "unsupported_capability" if method is not allowlisted; "server_crashed" if the session is not running; "request_timeout" if no response arrives within timeout_s; "protocol_error" if the server answers with a JSON-RPC error.
  • asyncio.CancelledError — If the awaiting task is cancelled; the pending future is cleaned up before re-raising.

Method PyrightSession.sync_documents

Open/change/close exact on-disk documents with explicit versions.

Closes any currently open document not present in sources (it is not needed by the current operation), then opens or updates every requested document: a first sighting of a path sends textDocument/didOpen; a strictly higher document_version for an already-open path sends textDocument/didChange with the full new text; a repeated (document_version, sha256) pair is a no-op that preserves any cached diagnostics for warm reuse.

sync_documents(sources: list[SourceState], texts: dict[str, str]) -> None

Parameter Type Default Description
sources list[SourceState] The exact on-disk identity (path, hash, monotonic version) of every document this operation needs open. Must not exceed :data:_MAX_OPEN_DOCUMENTS distinct paths.
texts dict[str, str] The exact decoded on-disk text for every path in sources, keyed by SourceState.path.

Raises:

  • LSPFailure — "server_crashed" if the session is not running; "resource_limit" if sources names more than :data:_MAX_OPEN_DOCUMENTS distinct paths; "invalid_request" if texts is missing a requested path, a repeated version's hash does not match the already-open document, or document_version regresses.

Method PyrightSession.diagnostics

Return version-matched publications and explicit missing/unversioned coverage.

Waits until every path in sources has a buffered textDocument/publishDiagnostics publication whose version matches that path's currently open document version (via :meth:sync_documents), or until timeout_s elapses — never inferring completeness from silence, a timer, or any other response type. A path whose only publication omitted its version is reported as unversioned rather than matched. A path already covered by a warm, still-matching per-path cache entry (an unchanged source state since the last call) is resolved immediately without waiting. The combined raw diagnostic count is capped at :data:_MAX_RAW_DIAGNOSTICS; paths dropped to respect the cap are reported as missing rather than truncated in place.

diagnostics(sources: list[SourceState], timeout_s: float) -> DiagnosticBatch

Parameter Type Default Description
sources list[SourceState] The exact document identities (path, hash, version) to collect diagnostics for; must match what the caller most recently passed to :meth:sync_documents.
timeout_s float Bound on waiting for every path's publication.

Returns: DiagnosticBatch — class:~parrot_tools.lsp.models.DiagnosticBatch that is complete only when every requested path has a matched, within-cap publication.

Raises:

  • LSPFailure — "server_crashed" if the session is not running.

Method PyrightSession.close

Shut the owned process down through a bounded, idempotent sequence.

Attempts a graceful shutdown request followed by an exit notification (best-effort, bounded by shutdown_timeout_s), then always escalates through terminate -> kill -> reap (each bounded), cancels the draining tasks, and fails every pending request future with "server_crashed". Calling :meth:close on a session that never started, or more than once, is a no-op.

close() -> None

Architecture

Clients

Bots & Agents

Memory

Knowledge

Tools & Toolkits

Flows

Experts

Skills

Stores & Loaders

MCP & A2A

Server & APIs

Integrations

Outputs & A2UI

Prompts

Security

Operations

Migration

SDD Tooling

LLM Wiki

Deployment

Decisions

API Reference

Clone this wiki locally