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>
108 lines
3.7 KiB
Python
108 lines
3.7 KiB
Python
from collections.abc import Awaitable, Callable, Mapping
|
|
from dataclasses import dataclass
|
|
|
|
from mirage.commands.builtin.utils.output import format_records
|
|
from mirage.commands.config import CommandOpts
|
|
from mirage.commands.errors import UsageError
|
|
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
|
|
from mirage.utils.errors import FS_ERRORS, format_fs_error
|
|
|
|
|
|
async def cmp_cmd(
|
|
paths: list[PathSpec],
|
|
*,
|
|
read_bytes: Callable[..., Awaitable[bytes]],
|
|
silent: bool = False,
|
|
verbose: bool = False,
|
|
limit: int | None = None,
|
|
print_bytes: bool = False,
|
|
skip: int | None = None,
|
|
) -> tuple[ByteSource | None, IOResult]:
|
|
if len(paths) > 2:
|
|
raise extra_operand_error(CommandName.CMP, paths[2].raw_path
|
|
or paths[2].virtual)
|
|
if len(paths) < 2:
|
|
raise UsageError("cmp: requires two paths")
|
|
p0, p1 = paths[0], paths[1]
|
|
try:
|
|
data1 = await read_bytes(p0)
|
|
data2 = await read_bytes(p1)
|
|
except FS_ERRORS as exc:
|
|
# GNU cmp reserves exit 1 for "files differ"; trouble (a missing
|
|
# or unreadable operand) is exit 2.
|
|
return None, IOResult(exit_code=2,
|
|
stderr=format_fs_error("cmp", exc, paths))
|
|
if skip is not None:
|
|
data1 = data1[skip:]
|
|
data2 = data2[skip:]
|
|
if limit is not None:
|
|
data1 = data1[:limit]
|
|
data2 = data2[:limit]
|
|
if data1 == data2:
|
|
return None, IOResult()
|
|
if silent:
|
|
return None, IOResult(exit_code=1)
|
|
if verbose:
|
|
out_lines: list[str] = []
|
|
for idx in range(min(len(data1), len(data2))):
|
|
if data1[idx] != data2[idx]:
|
|
out_lines.append(f"{idx + 1} {data1[idx]:o} {data2[idx]:o}")
|
|
return format_records(out_lines), IOResult(exit_code=1)
|
|
for idx in range(min(len(data1), len(data2))):
|
|
if data1[idx] != data2[idx]:
|
|
line = 1 + data1[:idx].count(ord(b"\n"))
|
|
msg = (f"{p0.virtual} {p1.virtual}"
|
|
f" differ: char {idx + 1}, line {line}")
|
|
if print_bytes:
|
|
msg += (f" is {data1[idx]:o} {chr(data1[idx])}"
|
|
f" {data2[idx]:o} {chr(data2[idx])}")
|
|
return format_records([msg]), IOResult(exit_code=1)
|
|
shorter = p0.virtual if len(data1) < len(data2) else p1.virtual
|
|
msg = f"cmp: EOF on {shorter}"
|
|
return format_records([msg]), IOResult(exit_code=1)
|
|
|
|
|
|
__all__ = ["cmp_cmd"]
|
|
|
|
|
|
@dataclass(frozen=True, slots=True)
|
|
class CmpFlags:
|
|
silent: bool = False
|
|
verbose: bool = False
|
|
limit: int | None = None
|
|
print_bytes: bool = False
|
|
skip: int | None = None
|
|
|
|
|
|
def parse_flags(flags: Mapping[str, FlagValue]) -> CmpFlags:
|
|
fl = FlagView(flags, spec=SPECS["cmp"])
|
|
n_raw = fl.as_str("n")
|
|
i_raw = fl.as_str("i")
|
|
return CmpFlags(
|
|
silent=fl.as_bool("s"),
|
|
verbose=fl.as_bool("args_l"),
|
|
limit=int(n_raw) if n_raw is not None else None,
|
|
print_bytes=fl.as_bool("b"),
|
|
skip=int(i_raw) if i_raw is not None else None,
|
|
)
|
|
|
|
|
|
async def cmp_generic(
|
|
paths: list[PathSpec],
|
|
texts: list[str],
|
|
opts: CommandOpts,
|
|
read_bytes: Callable[..., Awaitable[bytes]],
|
|
) -> tuple[ByteSource | None, IOResult]:
|
|
parsed = parse_flags(opts.flags)
|
|
return await cmp_cmd(paths,
|
|
read_bytes=read_bytes,
|
|
silent=parsed.silent,
|
|
verbose=parsed.verbose,
|
|
limit=parsed.limit,
|
|
print_bytes=parsed.print_bytes,
|
|
skip=parsed.skip)
|