3293cb5ff4
Two cleanup-plan items, both Python-side.
Item 29 -- `mirage mcp`. TypeScript shipped a six-tool stdio MCP server;
Python had none, so a pip-install user could not point Cursor or Claude
Desktop at a workspace and `mirage --help` differed by distribution.
Adding the entry point alone would have duplicated the tools, because
this side kept them private inside the Claude Agent SDK integration, so
the shared layer comes first:
- agents/tool_descriptions.py -- the six strings, one copy.
- agents/tool_operations.py -- MirageToolOperations, lifted out of the
SDK server's private _MirageTools.
- agents/file_version.py -- stale-write protection, which this side
lacked entirely. TS stamps stored bytes; here the stamp covers the
rendered bytes, because this read tool has always rendered and an
edit must search what the agent was actually shown.
- agents/mcp/server.py + cli/mcp.py -- the server and `mirage mcp`.
- server/workspace_config.py -- config discovery (candidates, env
names, walk up from cwd), which Python had nowhere, so every entry
point had to be handed an explicit path.
The server is the low-level MCP Server rather than FastMCP: FastMCP does
not forward a version, and TS advertises one. Handlers are bound methods,
not decorated closures, so nothing nests.
Item 28 -- the mypy allowlist. 54 modules opted *in* to annotation
checking against 1826, so the default was unchecked and every new file
joined the unchecked side. The default is now strict, with a list of
what is not yet annotated that only shrinks. 166 annotations cleared
along the way; the remainder is named module by module.
Two real defects surfaced by the annotations, neither of them typing:
- Workspace._original_open / _original_os were invented by assignment
in lifecycle.patch_process, so unpatch without a patch raised
AttributeError. Declared, and the restore is guarded.
- sed_generic declared a non-optional writer while its own docstring
and its `write_bytes is None` branch said otherwise; the builder
passes None whenever the backend cannot write.
Tests keep the PathSpec rule instead of full strict: measured, full
strict on python/tests is 2374 errors, of which 634 are `str` where a
pydantic field declares SecretStr -- which pydantic coerces at runtime --
and most of the rest is the monkeypatched-fake pattern CLAUDE.md
sanctions. The rule that is violated for real is PathSpec, 19 times, and
scripts/check_test_pathspec.py now holds that line. One of the 19 was a
latent AttributeError: tests/e2e passes a str to s3 write_bytes, which
reads .mount_path, and the test skips without a live versioned bucket.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
99 lines
3.2 KiB
Python
99 lines
3.2 KiB
Python
import base64 as b64lib
|
|
from collections.abc import AsyncIterator, Callable, Mapping
|
|
from dataclasses import dataclass
|
|
|
|
from mirage.commands.builtin.utils.stream import _resolve_source
|
|
from mirage.commands.config import CommandOpts
|
|
from mirage.commands.spec import SPECS
|
|
from mirage.commands.spec.types import CommandName, FlagValue, FlagView
|
|
from mirage.commands.spec.usage import extra_operand_error
|
|
from mirage.io.types import ByteSource, IOResult
|
|
from mirage.types import PathSpec, ReadStreamFn
|
|
|
|
|
|
async def _base64_encode_stream(source: AsyncIterator[bytes],
|
|
wrap: int | None) -> AsyncIterator[bytes]:
|
|
buf = b""
|
|
async for chunk in source:
|
|
buf += chunk
|
|
encoded = b64lib.b64encode(buf).decode()
|
|
if not encoded:
|
|
return
|
|
if wrap is not None and wrap == 0:
|
|
yield encoded.encode() + b"\n"
|
|
return
|
|
line_len = wrap if wrap is not None else 76
|
|
lines: list[str] = []
|
|
for i in range(0, len(encoded), line_len):
|
|
lines.append(encoded[i:i + line_len])
|
|
yield "\n".join(lines).encode() + b"\n"
|
|
|
|
|
|
async def _base64_decode_stream(source: AsyncIterator[bytes],
|
|
ignore_garbage: bool) -> AsyncIterator[bytes]:
|
|
buf = b""
|
|
async for chunk in source:
|
|
buf += chunk
|
|
text = b"".join(buf.split())
|
|
yield b64lib.b64decode(text, validate=not ignore_garbage)
|
|
|
|
|
|
async def base64_cmd(
|
|
paths: list[PathSpec],
|
|
*,
|
|
read_stream: Callable[..., AsyncIterator[bytes]],
|
|
stdin: ByteSource | None = None,
|
|
decode: bool = False,
|
|
wrap: int | None = None,
|
|
ignore_garbage: bool = False,
|
|
) -> tuple[ByteSource | None, IOResult]:
|
|
if len(paths) > 1:
|
|
raise extra_operand_error(CommandName.BASE64, paths[1].raw_path
|
|
or paths[1].virtual)
|
|
cache: list[str] = []
|
|
if paths:
|
|
source: AsyncIterator[bytes] = read_stream(paths[0])
|
|
cache = [paths[0].mount_path]
|
|
else:
|
|
source = _resolve_source(stdin)
|
|
|
|
if decode:
|
|
return _base64_decode_stream(source,
|
|
ignore_garbage), IOResult(cache=cache)
|
|
return _base64_encode_stream(source, wrap=wrap), IOResult(cache=cache)
|
|
|
|
|
|
__all__ = ["base64_cmd"]
|
|
|
|
|
|
@dataclass(frozen=True, slots=True)
|
|
class Base64Flags:
|
|
decode: bool = False
|
|
wrap: int | None = None
|
|
ignore_garbage: bool = False
|
|
|
|
|
|
def parse_flags(flags: Mapping[str, FlagValue]) -> Base64Flags:
|
|
fl = FlagView(flags, spec=SPECS["base64"])
|
|
wrap_value = fl.as_str("wrap")
|
|
return Base64Flags(
|
|
decode=fl.as_bool("D") or fl.as_bool("decode"),
|
|
wrap=int(wrap_value) if wrap_value is not None else None,
|
|
ignore_garbage=fl.as_bool("ignore_garbage"),
|
|
)
|
|
|
|
|
|
async def base64_generic(
|
|
paths: list[PathSpec],
|
|
texts: list[str],
|
|
opts: CommandOpts,
|
|
read_stream: ReadStreamFn,
|
|
) -> tuple[ByteSource | None, IOResult]:
|
|
parsed = parse_flags(opts.flags)
|
|
return await base64_cmd(paths,
|
|
read_stream=read_stream,
|
|
stdin=opts.stdin,
|
|
decode=parsed.decode,
|
|
wrap=parsed.wrap,
|
|
ignore_garbage=parsed.ignore_garbage)
|