8000 Use one ANSI filter everywhere, and close the two logging gaps behind it · Issue #77 · nixidae/nixidae · GitHub
[go: up one dir, main page]

Skip to content

Use one ANSI filter everywhere, and close the two logging gaps behind it #77

Closed

Description

@Lillecarl

What

Use one ANSI filter everywhere, and make a machine check keep it that way.
Then close the two gaps in the logging path that this investigation exposed
and that are not about ANSI at all.

Why

Nix writes the escape sequences that reach this library, so Nix owns the answer
to which bytes are an escape sequence. nix::filterANSIEscapes in libutil is
that answer. This repository grew three separate answers instead.

a167255a removed the first one, the strip-ansi package, whose whole
implementation is the pattern \x1B\[\d+(;\d+){0,2}m. That reads one shape of
one sequence, an SGR sequence of at most three numeric parameters. Measured
against Nix's filter:

input the pattern gives Nix gives
\x1b]8;;http://x\x1b\a\x1b]8;;\x1b\ the input, unchanged a
\x1b[38;2;255;0;0mred\x1b[0m \x1b[38;2;255;0;0mred red
\x1b[2Kgone \x1b[2Kgone gone

Two answers remain, and neither is Nix's.

Facts that bound the design

Recorded because they close off approaches that look available:

  1. There is no plain rendering to ask Nix for. Colour enters at
    HintFmt::operator%, which wraps every interpolated value in Magenta
    before the message is stored (src/libutil/include/nix/util/fmt.hh). A
    nix::ErrorInfo holds escapes from birth. Even --log-format internal-json
    ships them, in both msg and raw_msg (JSONLogger::logEI). Filtering is
    the only option, and the only open question is where it happens.
  2. Nix filters in exactly one production place: the last line of
    SimpleLogger::log, filterANSIEscapes(s, !tty). LoggerSettings and
    globals.hh carry no colour setting.
  3. The environment lever does not reach this library. NO_COLOR and
    TERM=dumb feed a first-call-cached static const bool inside isTTY(),
    which only SimpleLogger consults. Measured: the text of an eval error is
    byte-identical with and without NO_COLOR=1 TERM=dumb.
  4. Filtering late costs almost nothing. Over 10000 messages: 9.41 ms for
    one call each, 9.28 ms for one bulk call, against a boundary floor of
    1.16 ms. A bulk entry point would save 1.4%, so this issue does not ask for
    one. Filtering therefore stays where the consumer wants it.

Work

1. One filter, reachable

nanopynix._ansi.strip_ansi is private, and the remaining call sites are in
other distributions. CLAUDE.md gives the remedy: a library capability of
general use belongs in the public API of nanopynix, rather than reached
through a private module.

  • Export strip_ansi from nanopynix, add it to __all__, and give it an
    autofunction directive under docs/nanopynix/api/.
  • nanopynix-helpers/src/nanopynix_helpers/fod.py drops _ANSI_ESCAPE and
    calls it. The substitution is safe: _FOD_MISMATCH matches leading
    whitespace with \s*, so the tab expansion of Nix's filter changes nothing,
    and a dropped carriage return helps a CRLF log.
  • tests/nanopynix/primops/test_primop_error_parity.py drops _ANSI and
    calls it.

2. A check that keeps it that way

Three implementations of one thing is the shape that tests/meta/ exists for.
Add a scanner and a conformance test: no module outside nanopynix-bindings
may compile a regular expression that contains an escape-sequence literal. A
test that constructs escapes as a fixture stays legal, because the ban is on
re-implementing the filter.

This generalises tests/nanopynix/test_hostile_inputs.py, which already
asserts that no colour code reaches one exception, and says so in prose:
"Nix's colour codes must not reach the exception".

3. PyLogger::logEI drops the structure it is given

This is the defect, and it is not about ANSI.
nanopynix-bindings/src/nix_util.cpp sends only ei.msg.str() to the Python
callback. The position, the trace, the suggestions and isFromExpr are
discarded at that line.

errinfo::to_dict in nix_error_info.hh already builds every one of them for
the exception path, and Nix's own JSONLogger::logEI emits every one of them.
So the log path of this library is less structured than Nix's own machine
format, while the error path is more structured. A consumer that wants a
position from a warning cannot have one.

This changes the log-event wire schema, so it needs its own design:

  • what LogEvent carries for an error action, beside the flat message;
  • whether the existing max_traces and max_hint_bytes caps of the error
    path apply to it;
  • what pynix and ekn render from it.

4. Escapes cost the error trailer 27% of its budget

Measured on one real eval error: 27% of the bytes of raw, info["msg"] and
the trace hints are escape bytes.

That payload rides grpc-status-details-bin, which has a size limit. The caps
in nix_error_info.hh exist for it (max_hint_bytes = 4096,
max_traces = 32), and _shrink in rpc/_status_details.py discards raw
outright when the trailer still does not fit.

Filtering before the trailer is encoded would buy back roughly a quarter of
that budget, and so more trace depth for the same header. This is a capacity
question and not a colour-policy question, because item 1 keeps filtering late
for every consumer that renders. Decide it separately.

Acceptance criteria

  • nanopynix.strip_ansi is public, documented, and covered by
    tests/meta/test_docs_coverage.py.
  • grep finds no escape-sequence literal inside a re.compile outside
    nanopynix-bindings, and a test enforces it.
  • The meta test fails against the tree before item 1, and passes after.
  • tests/nanopynix_helpers/test_fod.py carries a case with an OSC 8 hyperlink
    or a 24-bit colour, so the upgrade is pinned rather than only performed.
  • Items 3 and 4 each carry a decision, recorded here, before any code lands
    for them.

Activity

  1. Lillecarl commented on Aug 3, 2026

    @Lillecarl
    ContributorAuthor

    Work items 1 and 2 landed in 643d70e3 (refactor(nanopynix): one ANSI filter, and a gate that keeps it).

    • nanopynix.strip_ansi is public, and it has an autofunction directive in docs/nanopynix/api/session.md. The binding under it has one in bindings.md.
    • nanopynix_helpers.fod and tests/nanopynix/primops/test_primop_error_parity.py call it, and neither one carries a pattern now.
    • tests/meta/test_ansi_filtering.py is the machine check, with the scanner in tests/support/ansi_regexes.py. It fails against the tree before the change, and it names the file and the line.
    • tests/nanopynix_helpers/test_fod.py carries an OSC 8 hyperlink and a 24-bit colour, so the upgrade is pinned.

    Measured for the record. None of the three retired patterns removes an OSC 8 hyperlink, because an OSC sequence opens with ESC ] and each pattern reads ESC [. The strip-ansi package also left ESC [38;2;255;0;0m in place and kept ESC [2K, and the parity pattern kept ESC [2K.

    Items 3 and 4 are open. Each one needs its decision recorded here before code lands for it.

  2. Lillecarl commented on Aug 5, 2026

    @Lillecarl
    ContributorAuthor

    Items 1 and 2 are done

    643d70e and 8f9e82c landed them. Checked against the tree:

    • nanopynix.strip_ansi is public, is in __all__, and has an
      autofunction directive in docs/nanopynix/api/session.md:27.
    • tests/meta/test_ansi_filtering.py is the scanner, with the positive and
      the negative cases the acceptance criteria ask for: a plain literal, an
      f-string pattern, a function other than compile, a fixture, a docstring, a
      regex that is not about escapes, an escape passed as the subject, and
      re.escape of a sequence.
    • tests/nanopynix_helpers/test_fod.py:61-63 carries both an OSC 8 hyperlink
      and a 24-bit colour, so the upgrade is pinned.

    Item 3: carry the whole payload

    Decision: an error log event carries what errinfo::to_dict builds.

    errinfo::to_dict in nix_error_info.hh already builds level, msg,
    pos, is_from_expr, status, traces, truncated and suggestions for
    the exception path. The log path gets that same builder, so there is one
    implementation rather than two that must stay in step.

    The existing caps come with it: max_traces = 32 and max_hint_bytes = 4096.
    They are the caps the exception path already applies, and applying a different
    number to the same payload would need its own reason.

    The flat message stays where it is. LogEventExt.message reads args[-1],
    and pynix/src/pynix/_util.py:255 renders that and nothing else, so no
    consumer breaks. The structure arrives beside it.

    Item 4: do not filter the trailer

    Decision: no filtering before the trailer is encoded. Raise the caps if
    trace depth is the want.

    The measurement stands -- escapes are 27% of the bytes. The reason not to act
    on it is a different one.

    Item 1 decided that filtering stays late, for each consumer that renders.
    Filtering at the trailer would apply to the rpc path alone, because an inproc
    caller never encodes one. An rpc caller would then see uncoloured error text
    where an inproc caller sees colour, from the same library and the same call.
    That is an engine asymmetry, and this repository treats such an asymmetry as a
    defect unless process isolation forces it. A size budget is not that.

    _shrink in rpc/_status_details.py already discards raw first when the
    trailer does not fit, so the budget has a lever that costs no fidelity. If
    trace depth turns out to be short in practice, raise max_traces and measure
    again.

  3. added
    source: nanopynix
    on Oct 19, 2026
  4. added theissue type on Oct 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    source: nanopynix

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      0