Skip to content

Sanitize command names in HDR output file names - #629

Merged
fcostaoliveira merged 1 commit into
masterfrom
fix/hdr-filename-sanitize-627
Oct 14, 2026
Merged

fcostaoliveira merged 1 commit into
masterfrom
fix/hdr-filename-sanitize-627

Conversation

@fcostaoliveira

@fcostaoliveira fcostaoliveira commented Oct 14, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

--hdr-file-prefix uses the command display name as part of the per-command file names. With --scan-incremental-iteration, master (013cb85) writes:

h_SCAN 0_command_run_1.{txt,hgrm}
h_SCAN <cursor>_command_run_1.{txt,hgrm}

Windows reserves < and > in file names, so fopen() fails there and the histogram is lost after a single perror(). This will matter once the native Windows build from #553 lands. actions/upload-artifact also rejects these paths. Any command name containing < > : " / \ | ? *, a control character or a space has the same problem.

Root cause

run_stats::save_hdr_arbitrary_commands() passes command_name straight into "%s_%s_command_run_%d.{txt,hgrm}". The only mapping applied today is the _N dedup suffix from #587/#588.

Fix

  • New helper sanitize_file_name_component() replaces spaces, control characters (0x00-0x1f, 0x7f) and < > : " / \ | ? * with _. It is used only for the command part of the HDR file names. The prefix, stderr messages, .hgrm header and JSON keys keep the display name; the header uses the same #N form as the JSON keys.
  • No new collisions:
    • Sanitizing runs before the JSON output has duplicate keys when two --command lines share a name (rows silently dropped by parsers) #587 dedup pass, so names that sanitize to the same string get distinct _N suffixes. For example, A: and A? both become A_, and they are written as A__2 and A__3.
    • make_unique_labels() now skips an ordinal whose label is already taken by another name or label. Without that, a literal A__1 would be overwritten by the first suffixed A_. JSON keys use the same function, so they gain the same guarantee for the equivalent " #N" case.
  • The README now documents the file-name scheme and the replacement rule.

User-visible change

HDR file names change for any command whose name contains a space or one of the listed characters:

display name before after
SCAN 0 <p>_SCAN 0_command_run_1.hgrm <p>_SCAN_0_command_run_1.hgrm
SCAN <cursor> <p>_SCAN <cursor>_command_run_1.hgrm <p>_SCAN__cursor__command_run_1.hgrm
GET (Line 1) (--command-stats-breakdown=line) <p>_GET (Line 1)_command_run_1.hgrm <p>_GET_(Line_1)_command_run_1.hgrm

Plain names (GET, SET, GET_1, FULL_RUN) are unchanged. For the existing #587 duplicate-name case, the stderr "Writing ..." line and the .hgrm header now print the JSON-key form (GET #1) instead of the file-label form (GET_1). The file names are unchanged. Scripts that glob on the old names with spaces need updating.

Test evidence

Two new tests in tests/test_hdr_histogram.py:

  • test_hdr_file_names_are_sanitized_for_scan_cursor: SCAN with --scan-incremental-iteration over 200 keys. It asserts that no <prefix>_* file contains an unsafe character, and that the SCAN_0 and SCAN__cursor_ files exist. It skips on cluster, like test_scan.py, and cleans up its keys.
  • test_hdr_file_names_stay_unique_after_sanitizing: --command="A: __key__" --command-ratio=3 --command="A? __key__" --command="A__1 __key__" --command='A/\|*"x" __key__' with 120 requests. It asserts that no file contains an unsafe character and that each file holds the right sample count (A__2=60, A__3=20, A__1=20, A_____X_=20), so a file that was overwritten cannot pass. The fourth command covers / \ | * " (its display name is A/\|*"x").

Standalone redis, --env existing-env, 3 runs each:

binary sanitized_for_scan_cursor stay_unique_after_sanitizing
master 013cb85 3/3 FAIL (lists histogram_SCAN <cursor>_command_run_1.hgrm, ...) 3/3 FAIL (histogram_A:_..., histogram_A?_...)
this PR 3/3 PASS 3/3 PASS

The whole test_hdr_histogram.py module, including the #587 duplicate-name test, passed in 9 local runs with the fix. test_scan.py passed in 6 runs. CI covers the TLS, cluster and sanitizer matrix.

Fixes #627. Related: #553 (native Windows build), #587/#588 (duplicate-name _N suffixes).

Affected versions

2.3.0 through 2.5.2 (latest) and master, as recorded in #627:

🤖 Generated with Claude Code


Note

Low Risk
Scoped to HDR file path construction and label deduplication; JSON and console output keep display names. Filename changes may affect scripts that glob old paths with spaces or angle brackets.

Overview
HDR per-command output files (--hdr-file-prefix) now use sanitized command labels in .txt / .hgrm paths only: spaces, control characters, and < > : " / \ | ? * become _, while stderr, JSON keys, and histogram headers still use the original display names.

Sanitization runs before the existing _N duplicate-name logic, so names that collapse to the same string (e.g. A: and A? → A_) still get distinct files. make_unique_labels also skips suffix ordinals already taken by another command or label (fixes collisions with a literal name like A__1).

The README documents the naming scheme and examples (SCAN <cursor>, line-breakdown stats). RLTest adds coverage for --scan-incremental-iteration and post-sanitize uniqueness/sample counts.

User-visible: HDR filenames change when command names contained unsafe characters; plain names like GET / SET are unchanged.

Reviewed by Cursor Bugbot for commit 3032667. Bugbot is set up for automated code reviews on this repo. Configure here.

@github-actions

github-actions Bot commented Oct 14, 2026 •

Copy link
Copy Markdown

🤖 Automated first-pass review. A human maintainer's review is still required before merge.

Thanks for this. I went through the diff and the tests, and I have a few comments below.

The core fix looks right to me. I like that sanitizing happens before the #587 dedup pass, and the used set in make_unique_labels() handles the literal A__1 case cleanly. I traced the second test by hand: [A_, A_, A__1, A_____X_] gives A__2/A__3, and the 60/20/20/20 counts match. The loop also always terminates, because each failed insert bumps seen[name]. Pinning sample counts per file so an overwritten file can't pass is a nice touch. The fails-on-master / passes-with-fix table is the same bar PR#364 already met here.

  • Backward compat: did you consider leaving spaces alone? I may be wrong, but I don't think a space is reserved on Windows or rejected by actions/upload-artifact (its invalid set is " : < > | * ? \r \n). If that's right, the space mapping is the only reason SCAN 0 → SCAN_0 and GET (Line 1) → GET_(Line_1) change. Those names work today and people may well have scripts globbing them. Dropping spaces from the set would limit the rename to names that are actually broken (SCAN <cursor> and anything with the reserved characters). That's the same "pick the less-breaking fix when both work" reasoning as PR#212, and PR#60's "minimize risk of breaking something". You did disclose it in the description, but I'd rather not break the working names if we don't have to. Is there a reason spaces need to go as well (e.g. some shell-quoting concern I'm missing)?

  • Related: the stderr "Writing ..." line and the .hgrm header now show GET #1 instead of GET_1 for the JSON output has duplicate keys when two --command lines share a name (rows silently dropped by parsers) #587 duplicate case. That isn't needed for the Windows fix as far as I can tell. Is anything downstream reading that header? If not, fine, but it might be better as its own small change than bundled in here.

  • Just checking: as the new comment says, the file labels and the JSON keys can now number duplicates differently (in your test, JSON A: #1 ends up in file A__2). The stderr line is the only place that ties the two together. Is that acceptable for anyone joining HDR files back to the JSON? Maybe it's worth one sentence in the README next to the new paragraph.

Nitpicking: strchr needs <string.h>/<cstring>. It's probably already pulled in transitively in run_stats.cpp, but worth a quick check since you added <set> explicitly.

The fix and tests look solid. I'd mainly like the spaces question answered before this merges, since that's what decides how many existing file names change.

(I'd file this as a comment, not a formal block.)

--hdr-file-prefix builds per-command file names from the display name, so
--scan-incremental-iteration writes "<prefix>_SCAN <cursor>_command_run_1.hgrm".
'<' and '>' are reserved file-name characters on Windows (fopen() fails
and the histogram is silently lost once the native build from #553 lands),
and actions/upload-artifact rejects them, so CI jobs cannot upload these
files.

Replace spaces, control characters and < > : " / \ | ? * with '_' in the
command part of the HDR file names. The prefix, the stderr messages, the
.hgrm header and the JSON keys keep the display name.

Sanitizing runs before the #587 duplicate-name pass, so names that map to
the same string (e.g. "A:" and "A?") still get distinct "_N" suffixes.
The suffix pass now also skips an ordinal whose label is already taken
by another name (e.g. a literal "A__1"), so a suffixed label can no
longer overwrite another command's file.

This changes user-visible file names: "SCAN <cursor>" becomes
SCAN__cursor_, "SCAN 0" becomes SCAN_0, and --command-stats-breakdown=line
names such as "GET (Line 1)" become GET_(Line_1).

Fixes #627

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@fcostaoliveira
fcostaoliveira force-pushed the fix/hdr-filename-sanitize-627 branch from 3c77b9e to 3032667 Compare October 14, 2026 10:15
@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

Thanks for the review. Answers point by point:

1. Spaces in the replacement set. You're right that Windows and actions/upload-artifact accept spaces, so #627 doesn't require replacing them. I replaced them on purpose: a space is the character that breaks unquoted paths in the usual post-processing (for f in $(ls h_*), xargs, unquoted globs in CI scripts), and SCAN <cursor> changes anyway. I don't claim full shell safety, since parentheses in GET_(Line_1) are kept because they are valid on Windows and in artifact uploads. The trade-off is the one you describe: SCAN 0 and GET (Line 1) also get new file names. If maintainers prefer the less-breaking option, as in #212, the change is to drop ' ' from sanitize_file_name_component() and update the README sentence and the before/after table. I'll make that change if asked.

2. stderr / .hgrm header now use GET #1 instead of GET_1. Yes, that was intended. Both now print the same label as the JSON key, and the file name carries the file label. I've added this to the "User-visible change" section of the PR description, so anyone who parses the header will see it.

3. Display and file numbering can differ (e.g. A: #2 written to A__3). Agreed. The stderr line prints both names, and the code comment explains why the numbering can differ. I haven't added the README sentence in this push. I'll add it if maintainers want it in this PR. It would say that, for duplicate or sanitized-to-equal names, the _N in the file name is not guaranteed to match the #N in the JSON key, and that the stderr "Writing ..." line gives the mapping.

Other changes in this push:

  • Rebased onto current master (641aef2).
  • Closed a test gap: the uniqueness test now also runs --command='A/\|*"x" __key__'. Its display name contains / \ | * ". The test asserts that the file is A_____X_ with the expected sample count (20 of 120 requests) and that no HDR file name contains an unsafe character. Before this, a version that dropped those characters, or the control-character clause, from the set would still have passed. test_hdr_histogram.py passes locally (6/6, --env oss).

Interaction with #631. #631 (reserved-name fix: TOTAL/CPU collisions) changes the same functions, make_unique_labels() / unique_command_labels(). Whichever PR merges second must add the reserved names to the used set that this PR seeds in make_unique_labels(). Otherwise a suffixed or sanitized label could collide with a reserved key again.

🤖 Generated with Claude Code

@fcostaoliveira
fcostaoliveira merged commit f59e59e into master Oct 14, 2026
145 of 146 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant