Skip to content

rtk gain --failures (-F) panics on non-ASCII command text at gain.rs:731 (sibling of #2318) #3232

Closed

Description

@Samuel86-star

Bug Description

rtk gain --failures / rtk gain -F panics when a recorded command string in the parse-failure log contains a multi-byte (CJK) character and the display truncation slices it mid-character. This is the --failures display path, distinct from the --history path tracked in #2318 (which is gain.rs:250); this one is gain.rs:731.

Steps to Reproduce

Have any command containing CJK text in the failure log (e.g. a grep/find over a vault with Chinese paths, or pip output containing CJK), then:

rtk gain -F      # or: rtk gain --failures

Error Output

thread 'main' (8871270) panicked at src/analytics/gain.rs:731:50:
end byte index 37 is not a char boundary; it is inside '周' (bytes 35..38 of string)
note: run with RUST_BACKTRACE=1 environment variable to display a backtrace

(Different invocations land on different bytes/chars — e.g. earlier 跳 at byte boundary 21..24 — depending on which command string is being truncated.)

Root Cause

Same class as #2318: the truncation at gain.rs:731 uses a fixed byte index (&s[..n]) instead of a character-boundary-aware cut. CJK chars are 3 bytes in UTF-8, so a byte offset that is valid for ASCII lands mid-character.

Workaround

--format json renders the same data through a different code path and does not panic:

rtk gain -F --format json
rtk gain --history --format json

Suggested Fix

Use s.floor_char_boundary(max_len) (stable since Rust 1.82) or s.char_indices() before truncating — applies to both the :250 (--history) and :731 (--failures) sites.

Environment

  • rtk version: 0.42.3
  • OS: macOS (Darwin 25.5.0), arm64
  • Install: Homebrew
  • Locale: zh_CN.UTF-8

Related: #2318 (--history panic at gain.rs:250), #2851 (rtk find non-ASCII path panic).

Activity

  1. added
    bug
    good first issue
    on Jul 27, 2026
  2. pszymkowiak commented on Jul 27, 2026

    @pszymkowiak
    Collaborator

    wshm · Automated triage by AI

    🔍 Automated Triage

    🐛 Category bug
    🟠 Priority high
    🎯 Confidence 94%

    Summary

    rtk gain --failures panics on multi-byte (CJK) command text due to byte-index string slicing at gain.rs:731; fix with char-boundary-aware truncation (same fix pattern as #2318 for gain.rs:250).

    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.

  3. msranjana commented on Jul 27, 2026

    @msranjana

    Hi @pszymkowiak I would like to work on this issue. Please let me know if it's available for me to take. Thanks!

  4. JJordan0C commented on Jul 28, 2026

    @JJordan0C

    I initially investigated this issue with the intention of implementing a fix and opening a PR. However, after upgrading Homebrew to rtk 0.44.0, I could not reproduce the panic, so I dug into the repository history and performed a controlled before/after reproduction.

    Investigation

    PR #2789 (fix(analytics): truncate display strings on UTF-8 char boundaries) was merged into develop on July 3 and is included in the stable v0.44.0 tag. It replaces the byte-index truncation in both gain --history and gain --failures with the shared UTF-8-safe truncate() helper.

    Exact reproduction against v0.42.3

    I downloaded the official rtk-aarch64-apple-darwin.tar.gz asset for v0.42.3 and verified its SHA-256 against the release checksum:

    d47823afb25919e4e60838c5622e88ffa6536bc0b36a34a3f928bdccac40f614
    

    Test environment: macOS arm64. I used an isolated RTK_DB_PATH, so no existing analytics data was involved.

    I created a real temporary vault containing:

    /tmp/aaaaaaaaaaaaaaaa/周报/记录.txt
    

    Then ran the v0.42.3 binary with:

    rtk grep -r test /tmp/aaaaaaaaaaaaaaaa/周报

    The native grep fallback executed successfully and the command was recorded in the parse-failure log. The path length was chosen deliberately: in the recorded raw command, 周 occupies bytes 35..38, so the old 37-byte display slice lands inside the character.

    Running:

    rtk gain -F

    with v0.42.3 reproduced the report exactly:

    thread 'main' panicked at src/analytics/gain.rs:731:50:
    end byte index 37 is not a char boundary; it is inside '周' (bytes 35..38 of string)
    

    The process exited with code 134.

    Same database with v0.44.0

    Without changing or recreating the database entry, I ran the Homebrew v0.44.0 binary against the same database:

    rtk gain -F

    It completed with exit code 0 and displayed the CJK command correctly in both sections:

    Top Commands (by frequency)
         1x  grep -r test /tmp/aaaaaaaaaaaaaaaa/周报
    
    Recent Failures (last 10)
      ... [ok] grep -r test /tmp/aaaaaaaaaaaaaaaa/周报
    

    Additional observation

    Using the exact workaround syntax shown in the issue:

    rtk gain -F --format json

    still reached the text display path and panicked on the official v0.42.3 binary in this reproduction.

    Conclusion

    The bug is definitely present in v0.42.3, but the same deterministic reproduction is fixed in v0.44.0. A new PR appears unnecessary because #2789 already contains the relevant fix and it has shipped in v0.44.0. This issue can likely be closed as fixed, subject to maintainer confirmation. @pszymkowiak

  5. KuSh commented on Sep 5, 2026

    @KuSh
    Collaborator

    This looks resolved on the current code (PR #2789 / v0.44.0), but it could not be fully reproduced here. Marking it resolved-pending-close: it will be closed in a while unless someone reports it still happening on the latest version.

  6. added
    resolved-pending-close
    on Sep 5, 2026
  7. KuSh commented on Sep 13, 2026

    @KuSh
    Collaborator

    Fixed by #2789, shipped in v0.44.0: both the --history and --failures display paths now truncate on character boundaries instead of byte indices. I reproduced your case by seeding a failure log entry with CJK text and running rtk gain -F on current code, it truncates cleanly and exits 0. Please reopen or comment if you still hit a panic 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:highresolved-pending-close

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions