Skip to content

panic: rtk gain --history crashes on non-ASCII (Cyrillic) command strings — UTF-8 char boundary #2954

Closed

Description

@IlyaZaigralov

Summary

rtk gain --history panics with a UTF-8 char-boundary error when the recorded command string contains multi-byte characters (e.g. Cyrillic) near the history-display truncation width. The compression hook itself works fine — only the --history analytics view crashes.

Panic

thread 'main' panicked at src/analytics/gain.rs:236:54:
byte index 22 is not a char boundary; it is inside 'с' (bytes 21..23) of `rtk proxy grep -iE успешно установлено /dev/null`
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Minimal repro

# Run any rtk-proxied command whose text contains multi-byte UTF-8 (Cyrillic here)
# positioned so the ~22-byte truncation boundary lands inside a char:
rtk proxy grep -iE "успешно установлено" /dev/null

# Now view history — panics:
rtk gain --history

Expected

rtk gain --history truncates the command string for display on a character boundary and prints the row (optionally with an ellipsis), regardless of the command's encoding.

Actual

Byte-index slice at src/analytics/gain.rs:236 cuts a multi-byte char in half → byte index N is not a char boundary panic → the whole --history command aborts. Any command with Cyrillic/emoji/CJK/accented text near the truncation width makes history permanently unviewable until that entry rotates out.

Likely cause & fix

Line ~236 truncates with a raw byte slice like &cmd[..N]. Use a char-safe truncation instead, e.g.:

let shown: String = cmd.chars().take(N).collect();
// or find a safe boundary: let end = cmd.char_indices().take_while(|(i, _)| *i < N).last()...
// or the unicode-width crate for display-width-correct truncation.

Environment

  • rtk 0.34.3
  • macOS (Darwin arm64)
  • Locale with non-ASCII command output (Russian/Cyrillic)

Activity

  1. albatrossflyon-coder commented on Jul 14, 2026

    @albatrossflyon-coder
    Contributor

    Hi! I'd like to take this one. Could you assign it to me?

  2. added
    bug
    good first issue
    on Jul 14, 2026
  3. pszymkowiak commented on Jul 14, 2026

    @pszymkowiak
    Collaborator

    wshm · Automated triage by AI

    🔍 Automated Triage

    🐛 Category bug
    🟠 Priority high
    🎯 Confidence 97%

    Summary

    rtk gain --history panics on multi-byte UTF-8 command strings due to a raw byte-index slice at gain.rs:236; switch to a char-boundary-safe truncation.

    This looks like a simple fix that could be auto-resolved. Repo maintainers can use /wshm fix to attempt it.

    📁 Relevant files
    • src/analytics/gain.rs

    Triaged automatically by wshm · This is an automated analysis, not a human review.

  4. albatrossflyon-coder commented on Jul 17, 2026

    @albatrossflyon-coder
    Contributor

    Went to pick this up and traced it before writing anything — looks like this is already fixed. src/core/utils::truncate() and analytics/gain.rs::truncate_for_column() both already use .chars().take(n) (char-safe), landed in PR #2789 (fix/analytics-utf8-char-boundary-panic, commit 47b22e0, merged e200764).

    Reproduced live against current develop with a fresh release build using the exact repro from this issue:

    $ rtk proxy grep -iE "успешно установлено" /dev/null
    $ rtk gain --history
    ...
    07-16 22:34 • rtk proxy grep -iE усп... -0% (0)
    ...
    exit 0, no panic
    

    The Cyrillic command truncates and displays correctly. Given the version in the original report (0.34.3) predates the fix, this looks like it was resolved in a later release before this issue was triaged. Happy to be corrected if there's a different repro path that still panics on current develop — but wanted to report honestly rather than submit a patch for something that's already fixed.

  5. IlyaZaigralov commented on Jul 17, 2026

    @IlyaZaigralov
    Author

    Confirmed locally after upgrading from 0.34.3 to the latest stable v0.43.0: the exact
    reproduction still panics at gain.rs:250 with the same UTF-8 char-boundary error (exit
    134). Regular rtk gain exits 0.

    So the fix is confirmed on develop via #2789, but it is not present in the latest stable
    release yet. This should be resolved for stable users with v0.44.0.

  6. HetCreep commented on Aug 4, 2026

    @HetCreep

    This thread has two contradictory reports in it, and the sibling #3232 has a third. I read the code at v0.44.2 (tag v0.44.2, commit 700bdde), which is newer than any version mentioned above, and I think it resolves the disagreement.

    Provenance: AI-assisted source audit, human-directed. This is a source read at 700bdde — I did not run rtk gain --history with CJK/Cyrillic history myself, so please treat it as "the unsafe slice is gone from the code" rather than "I reproduced the absence of the crash".

    Both gain.rs panic sites look fixed at v0.44.2

    • gain.rs:362-373 truncate_for_column — now text.chars().take(width).collect() and text.chars().take(width - 3).collect(). Character-based, so no byte-boundary slice remains. This is the path behind the gain.rs:236/:250 panic in this issue.
    • gain.rs:730 — now &rec.timestamp[..rec.timestamp.floor_char_boundary(16)]. This is the gain.rs:731 site from rtk gain --failures (-F) panics on non-ASCII command text at gain.rs:731 (sibling of #2318) #3232.
    • core/utils.rs:29 truncate — s.chars().count() plus chars().take(max_len - 3). Also character-based.

    Grepping gain.rs for a raw [.. slice returns only the floor_char_boundary line above. So @albatrossflyon-coder's trace matches the current code; @IlyaZaigralov's reproduction was on v0.43.0, which predates it. If someone can confirm on v0.44.2, this and #3232 both look closeable.

    The same class survives at two other sites, and I do not see issues for either

    This is the reason I am posting rather than just noting "fixed":

    1. src/core/utils.rs:170 truncate_iso_date — guards on byte length, then slices by bytes:

    pub fn truncate_iso_date(date: &str) -> &str {
        if date.len() >= 10 { &date[..10] } else { date }
    }

    date.len() is bytes, &date[..10] is a byte index — the exact shape that was fixed in gain.rs. It is reached from seven call sites in src/cmds/cloud/aws_cmd.rs (:645, :649, :670, :674, :782, :867, :958, :997, :1299), each on a date field from an AWS-style JSON response. Reproduced with a verbatim extract:

    panicked at utils.rs:170: byte index 10 is not a char boundary
    

    2. src/cmds/js/prisma_cmd.rs:319-321 — a different failure mode, out-of-bounds rather than mid-character:

    let end = line[pos..].find(|c: char| c.is_whitespace()).unwrap_or(20);
    latest_migration = line[pos..pos + end].to_string();

    When the 202… token is the last thing on the line and shorter than 20 bytes, pos + end runs past the end of the string. Reproduced:

    panicked at prisma_cmd.rs: end byte index 59 is out of bounds for string of length 47
    

    Note that prisma_cmd.rs:252, sixty-odd lines earlier in the same file, already uses the correct unwrap_or(line.len() - pos).

    Also src/cmds/cloud/aws_cmd.rs:1342 — &id[..id.len().min(8)] with the comment // UUIDs are ASCII-safe, which is an assumption rather than a check; that one only triggers against a non-AWS SQS-compatible endpoint, so it is much narrower than the two above.

    Suggestion

    Since floor_char_boundary is already used correctly in-tree (gain.rs:730, session_cmd.rs:113), the fix pattern exists — it just has not reached these call sites. A repo-wide sweep for [.. on a &str with a .len() guard would probably find any remaining ones faster than filing them one at a time; there is currently no clippy::string_slice or clippy::indexing_slicing lint configured to catch new ones.

    Happy to open separate issues for the two sites above if that is preferred over one sweep.

  7. albatrossflyon-coder commented on Aug 5, 2026

    @albatrossflyon-coder
    Contributor

    Filed the two new bugs @HetCreep found separately, per their suggestion, both reproduced live on current develop (3044911): #3444 (truncate_iso_date) and #3445 (prisma_cmd.rs migration-name slice). Will pick up fixes for both after wrapping up some other work in progress.

  8. KuSh commented on Sep 5, 2026

    @KuSh
    Collaborator

    Fixed by #2789 and shipped in the 0.44.x line: the history view now truncates on character boundaries. Reproduced your exact steps on the current build — rtk proxy grep -iE "успешно установлено" /dev/null followed by rtk gain --history prints the row truncated as 'rtk proxy grep -iE усп...' and exits 0 with no panic. Please reopen or comment if you still hit it on the latest version.

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

    area:clibuggood first issuepriority:high

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions