Files
strukto-ai--mirage/python/mirage/commands/builtin/utils/formatting.py
T
bytecii 73f5bd7c21 chore: cleanup items 31+32 — nested-function rule, layout, CI audit, ls facets
Closes the two remaining small items of Block E. Both were largely
recon: much of what they filed had already been fixed, and the part
that had not turned up a real GNU divergence.

Item 31 (T3-7), rule/doc reconciliation
---------------------------------------

The `object`-parameter half is already done: #825's mypy inversion took
the package from the filed 28 down to 7, of which 3 are docstring prose
and the remaining 4 are protocol methods (`__contains__`, `pop`) whose
signatures typeshed dictates, all already listed in the no-object gate.
`utils/errors.py` reads `str | PathSpec` today, and `builders/sed.py` no
longer holds flag values at all -- item 22 moved that to the generic.

That left the nested-function rule, which the plan asked to decide
before gating. Measured: 39 nested defs, and **every one of them closes
over its enclosing scope** -- 38 by free variable, and `sed_helper._repl`
by binding through parameter defaults, the loop-variable idiom, with a
comment saying so. A flat "do not nest" is a rule the architecture
cannot keep (op factories, provision builders, the read-through cache,
every decorator's wrapper), so gating it as written would have meant 39
standing violations.

So the rule now says what it was reaching for: a nested def must capture
the scope around it, or it belongs at module level. That is mechanical
and enforceable -- `tests/test_nested_functions_are_closures.py` reads
free variables from `symtable` and parameter defaults from the AST. It
passes on the tree as-is and fires on a helper that reads only its own
arguments.

Layout: the eight test-only TS directories are flattened
(`ram/{cat,cut,grep,head,ls,tail,wc}/`, `ssh/ls/`), `awk_helper.ts` moves
up beside its nine siblings, and the `provision.ts` / `_provision.ts`
split is settled on Python's convention -- `_provision` for a backend's
own, bare `provision` for the shared ones (cli, generic_bind). That last
one was not cosmetic: Python already had `_provision.py` for github,
gmail, redis and email, so renaming **closed 4 real parity divergences**
(layout baseline 296 -> 292). The `findEval`/`findParse` line was
already done in #827.

Item 32 (T3-8), test/CI tidiness
--------------------------------

The two asymmetric `ls` conformance matrices are raised to symmetry, and
they pass: python `[ram,disk,redis]` vs typescript `[ram]` was stale
caution, not a divergence. Both runners now reject an asymmetric matrix
at load time unless the case carries a `divergence` key explaining why --
a case is a parity claim, and narrowing one side reads as coverage while
the side that still lists the backend goes green. Loading the corpus
under the new assertion proves no other case was asymmetric. The README
documented the override as "not yet needed"; it exists now.

Adds the `ts-audit` job, mirroring `test_python.yml`'s exactly (same
`continue-on-error`, same out-of-gate placement). Nothing ran `pnpm
audit` for the TS tree while `typescript/package.json` carried 27
hand-written CVE overrides that only a person remembering to check kept
current.

Integ facets for `ls`: `-A`, `-d`, `-r`, `-S`, `-h`, all pinned against
GNU coreutils 9.7 in docker. `-S` uses regular files only, because GNU
sorts a directory by its inode size while mirage counts it as 0 -- a
divergence CLAUDE.md documents deliberately.

The `-h` facet found a real bug
-------------------------------

`ls -h` disagreed with GNU three ways, and because the flag had zero
cross-backend coverage nothing caught it. GNU prints a count below one
unit with no suffix (`24`); mirage printed `24B`. GNU rounds *up* to the
precision shown (1025 bytes is `1.1K`); mirage gave `1.0K`. GNU drops
the decimal once the value reaches ten (`10K`); mirage gave `10.0K`.

One shared engine replaces both formatters in both languages -- GNU runs
`-h` and `-H` through one `human_readable`, and so do we now. Rounding
up can carry past the base (1048575 ceils to 1024K, which GNU shows as
`1.0M`), so the unit is re-chosen after rounding rather than once up
front. 21 GNU-read points are pinned as a table in each language.

Blast radius, all re-pinned: five integ cases that had recorded the `B`
suffix (du, discord, langfuse, email, gmail), and the du fan-out tests.
Those last ones needed care rather than a new number: they exist to
prove the total is humanized once instead of twice, and their 1500+1500
stops discriminating under correct rounding, since 3000 bytes and two
`1.5K` readings both render `3.0K`. They now use 1025+1025, where
single-rounding gives `2.1K` and double-rounding would give `2.2K`.

Verified: pre-commit clean, integ 8054/0 on ram+disk+redis in both
languages, conformance green in both, and the layout, spec, barrel,
docs, case-target and PathSpec gates all pass.

Not covered, still open on item 32: integ facets for `ls -t` (mtime
order is not stable across backends without a seeded fixture), mktemp,
gzip, the checksum `--check` companions, `df -H/-k/-a`, and `cmp
-n/-b/-i`; and the T2-7 helper unit-suite mirroring. Item 31 leaves the
provision *presence* sets unreconciled (Python has lancedb/qdrant, TS has
trello) -- that is a question about which backends should provision at
all, not about naming.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-16 06:57:08 -07:00

151 lines
5.3 KiB
Python

# ========= Copyright 2026 @ Strukto.AI All Rights Reserved. =========
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ========= Copyright 2026 @ Strukto.AI All Rights Reserved. =========
from datetime import datetime, timezone
from mirage.commands.builtin.utils.constants import (DEFAULT_MODES,
EPOCH_LS_TIME, MONTHS,
NUMERIC_PREFIX,
TYPE_CHARS)
from mirage.types import LINK_TARGET_KEY, FileStat, FileType
def human_scaled(n: int, base: int, units: tuple[str, ...]) -> str:
"""GNU's ``human_readable`` rounding, shared by ``-h`` and ``-H``.
Three rules, none of which fall out of a plain divide-and-format.
Below one unit GNU prints the count alone -- ``24``, never ``24B``.
Above it the value is rounded *up* to the precision shown, so 1025
bytes is ``1.1K`` rather than ``1.0K``. And the decimal is dropped
once the scaled value reaches ten, giving ``10K`` rather than
``10.0K``. Rounding up can carry past the base (1048575 bytes ceils
to 1024K, which GNU shows as ``1.0M``), so the unit is re-chosen
after rounding instead of once up front.
Args:
n (int): byte count.
base (int): 1024 for ``-h``, 1000 for ``-H``.
units (tuple[str, ...]): suffixes indexed by power; index 0 is
unused because a sub-unit count carries no suffix at all.
Returns:
str: the size as GNU would print it.
"""
if n < base:
return str(n)
i, divisor = 1, base
while True:
tenths = -(-n * 10 // divisor)
if tenths < 100:
return f"{tenths // 10}.{tenths % 10}{units[i]}"
whole = -(-n // divisor)
if whole < base or i == len(units) - 1:
return f"{whole}{units[i]}"
i += 1
divisor *= base
def _human_size(n: int) -> str:
return human_scaled(n, 1024, ("", "K", "M", "G", "T", "P", "E"))
def _perm_triplet(bits: int, special: str | None = None) -> str:
if special is not None:
execbit = special.lower() if bits & 1 else special.upper()
else:
execbit = "x" if bits & 1 else "-"
return ("r" if bits & 4 else "-") + ("w" if bits & 2 else "-") + execbit
def _ls_mode_string(s: FileStat) -> str:
type_char = TYPE_CHARS.get(s.type, "-") if s.type is not None else "-"
default = DEFAULT_MODES.get(s.type, 0o644) if s.type is not None else 0o644
mode = s.mode if s.mode is not None else default
perms = (_perm_triplet(mode >> 6, "s" if mode & 0o4000 else None) +
_perm_triplet(mode >> 3, "s" if mode & 0o2000 else None) +
_perm_triplet(mode, "t" if mode & 0o1000 else None))
return f"{type_char}{perms}"
def _ls_time_string(modified: str | None) -> str:
if not modified:
return EPOCH_LS_TIME
try:
text = modified.replace("Z", "+00:00")
dt = datetime.fromisoformat(text).astimezone(timezone.utc)
except (ValueError, TypeError):
return EPOCH_LS_TIME
month = MONTHS[dt.month - 1]
day = f"{dt.day:>2}"
return f"{month} {day} {dt.hour:02d}:{dt.minute:02d}"
def _ls_name(s: FileStat) -> str:
"""The name column: GNU appends ``-> target`` for a symlink row.
Args:
s (FileStat): the row being rendered.
"""
if s.type != FileType.SYMLINK:
return s.name
target = s.extra.get(LINK_TARGET_KEY)
return f"{s.name} -> {target}" if target else s.name
def format_ls_long(
stats: list[FileStat],
*,
human: bool = False,
owner: str = "user",
group: str = "user",
size_width: int | None = None,
) -> list[str]:
sizes = [
_human_size(s.size or 0) if human else str(s.size or 0) for s in stats
]
width = size_width if size_width is not None else max(
(len(x) for x in sizes), default=1)
out: list[str] = []
for s, raw_size in zip(stats, sizes):
if s.size is None and s.modified is None:
mode = _ls_mode_string(s)
out.append(f"{mode}\t-\t-\t{_ls_name(s)}")
continue
mode = _ls_mode_string(s)
size = raw_size.rjust(width)
time = _ls_time_string(s.modified)
who = str(s.uid) if s.uid is not None else owner
grp = str(s.gid) if s.gid is not None else group
out.append(f"{mode} 1 {who} {grp} {size} {time} {_ls_name(s)}")
return out
def to_number(val: str) -> float:
"""Coerce a string to a number with GNU awk semantics.
Args:
val (str): raw token; the leading numeric prefix counts, else 0.
"""
m = NUMERIC_PREFIX.match(val.strip())
return float(m.group(0)) if m else 0.0
def format_number(val: float) -> str:
"""Render an awk numeric value, collapsing integral floats.
Args:
val (float): numeric value to render.
"""
return str(int(val)) if val == int(val) else str(val)