Repository navigation
Description
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:
- There is no plain rendering to ask Nix for. Colour enters at
HintFmt::operator%, which wraps every interpolated value inMagenta
before the message is stored (src/libutil/include/nix/util/fmt.hh). A
nix::ErrorInfoholds escapes from birth. Even--log-format internal-json
ships them, in bothmsgandraw_msg(JSONLogger::logEI). Filtering is
the only option, and the only open question is where it happens. - Nix filters in exactly one production place: the last line of
SimpleLogger::log,filterANSIEscapes(s, !tty).LoggerSettingsand
globals.hhcarry no colour setting. - The environment lever does not reach this library.
NO_COLORand
TERM=dumbfeed a first-call-cachedstatic const boolinsideisTTY(),
which onlySimpleLoggerconsults. Measured: the text of an eval error is
byte-identical with and withoutNO_COLOR=1 TERM=dumb. - 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_ansifromnanopynix, add it to__all__, and give it an
autofunctiondirective underdocs/nanopynix/api/. nanopynix-helpers/src/nanopynix_helpers/fod.pydrops_ANSI_ESCAPEand
calls it. The substitution is safe:_FOD_MISMATCHmatches 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.pydrops_ANSIand
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
LogEventcarries for anerroraction, beside the flat message; - whether the existing
max_tracesandmax_hint_bytescaps of the error
path apply to it; - what
pynixandeknrender 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_ansiis public, documented, and covered by
tests/meta/test_docs_coverage.py.grepfinds no escape-sequence literal inside are.compileoutside
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.pycarries 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
refactor(nanopynix): one ANSI filter, and a gate that keeps it
Lillecarl commentedon Aug 3, 2026
Work items 1 and 2 landed in 643d70e3 (
refactor(nanopynix): one ANSI filter, and a gate that keeps it).nanopynix.strip_ansiis public, and it has an autofunction directive indocs/nanopynix/api/session.md. The binding under it has one inbindings.md.nanopynix_helpers.fodandtests/nanopynix/primops/test_primop_error_parity.pycall it, and neither one carries a pattern now.tests/meta/test_ansi_filtering.pyis the machine check, with the scanner intests/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.pycarries 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 readsESC [. Thestrip-ansipackage also leftESC [38;2;255;0;0min place and keptESC [2K, and the parity pattern keptESC [2K.Items 3 and 4 are open. Each one needs its decision recorded here before code lands for it.
Lillecarl commentedon Aug 5, 2026
Items 1 and 2 are done
643d70eand8f9e82clanded them. Checked against the tree:nanopynix.strip_ansiis public, is in__all__, and has anautofunctiondirective indocs/nanopynix/api/session.md:27.tests/meta/test_ansi_filtering.pyis the scanner, with the positive andthe negative cases the acceptance criteria ask for: a plain literal, an
f-string pattern, a function other than
compile, a fixture, a docstring, aregex that is not about escapes, an escape passed as the subject, and
re.escapeof a sequence.tests/nanopynix_helpers/test_fod.py:61-63carries both an OSC 8 hyperlinkand a 24-bit colour, so the upgrade is pinned.
Item 3: carry the whole payload
Decision: an
errorlog event carries whaterrinfo::to_dictbuilds.errinfo::to_dictinnix_error_info.hhalready buildslevel,msg,pos,is_from_expr,status,traces,truncatedandsuggestionsforthe 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 = 32andmax_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.messagereadsargs[-1],and
pynix/src/pynix/_util.py:255renders that and nothing else, so noconsumer 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.
_shrinkinrpc/_status_details.pyalready discardsrawfirst when thetrailer 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_tracesand measureagain.
feat(bindings): give an error log event Nix's structured detail
feat(bindings): give an error log event Nix's structured detail
refactor(nanopynix): one ANSI filter, and a gate that keeps it
feat(bindings): give an error log event Nix's structured detail