Skip to content

API Reference parrot_tools.lsp.snapshot

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

parrot_tools.lsp.snapshot

View source

Bounded workspace snapshots and position conversion for the LSP pilot (FEAT-580, M1).

Two responsibilities live here, both pure/off-event-loop by construction:

  • :func:capture_workspace builds a deterministic manifest of the files a Pyright checkpoint depends on (Git tracked + non-ignored untracked .py/.pyi files, plus lock/config files), then hashes and optionally reads them in an owned subprocess so the event loop never blocks on file I/O. The subprocess performs a stable open/fstat/read/fstat sequence per file to detect ordinary concurrent edits, and is killed/reaped on timeout or cancellation.
  • :func:to_lsp_position / :func:from_lsp_range convert between the toolkit's public one-based Unicode coordinates and the zero-based UTF-16 coordinates the LSP wire protocol requires, including surrogate boundary handling.

Nothing in this module calls into Pyright or the LSP session (session.py, a later task); it only prepares the evidence those modules need.

Function capture_workspace

Return a bounded, stable workspace snapshot and digest.

Builds the deterministic manifest (Git tracked + non-ignored untracked .py/.pyi files, tracked deletions as tombstones, and in-root lock/Pyright-config files), then hashes/reads it off the event loop in an owned subprocess.

capture_workspace(config: LSPConfig, paths: list[str]) -> WorkspaceSnapshot

Parameter Type Default Description
config LSPConfig Trusted, validated toolkit configuration.
paths list[str] Repository-relative paths whose exact source text must be returned in requested_text (e.g. the file(s) a navigation or diagnostics call targets). Every entry must already be a member of the deterministic manifest.

Returns: WorkspaceSnapshot — class:WorkspaceSnapshot with a digest sensitive to every manifest file's content, tombstoned deletions, and the effective configuration.

Raises:

  • LSPFailure — With one fixed operational code — path_outside_root for traversal/escaping symlinks, unsupported_language for a requested non-Python/stub path, invalid_request for an ignored/untracked-and-unlisted requested path, file_missing for a requested tombstoned path, file_too_large/ workspace_limit for exceeded bounds, invalid_encoding for undecodable content, source_changed for a detected concurrent edit, request_timeout for a worker deadline miss, or resource_limit/protocol_error for a worker process/transport failure.
  • asyncio.CancelledError — Propagated after the owned worker process is killed and reaped.

Function from_lsp_range

Convert a zero-based UTF-16 LSP range back to a one-based Unicode :class:SourceRange.

from_lsp_range(text: str, path: str, lsp_range: dict[str, Any]) -> SourceRange

Parameter Type Default Description
text str The exact decoded file content the range is relative to.
path str Repository-relative POSIX path to attach to the result.
lsp_range dict[str, Any] A raw LSP Range object: {"start": {"line", "character"}, "end": {...}}.

Returns: SourceRange

Raises:

  • LSPFailure — position_out_of_range past the end of the file/line, or protocol_error for a negative coordinate or a position that splits a UTF-16 surrogate pair.

Function to_lsp_position

Convert a validated one-based Unicode position to zero-based UTF-16.

to_lsp_position(text: str, line: int, column: int) -> dict[str, int]

Parameter Type Default Description
text str The exact decoded file content the position is relative to.
line int One-based line number (a value one past the last line denotes the end-of-file position, valid only with column=1).
column int One-based Unicode code-point column; text[:column - 1] on the target line precedes it.

Returns: dict[str, int] — {"line": <zero-based line>, "character": <zero-based UTF-16 code unit>}.

Raises:

  • LSPFailure — position_out_of_range for a non-positive line/column or a position past the end of the file/line.

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