Skip to content

fix(read): only drop a line as a block comment when it starts with the marker - #4205

Closed
Protocol-zero-0 wants to merge 1 commit into
rtk-ai:developfrom
Protocol-zero-0:fix/read-block-comment-in-string
Closed

Protocol-zero-0 wants to merge 1 commit into
rtk-ai:developfrom
Protocol-zero-0:fix/read-block-comment-in-string

Conversation

@Protocol-zero-0

Copy link
Copy Markdown
Contributor

Fixes findings 1 and 2 of #3425 (finding 3, the docs claim for cat/head, is separate and untouched here).

Reproduced on develop (b748a5f)

fn main() {
    let p = Glob::new("src/*.rs");
    delete_all_user_data();
    println!("done");
}
$ rtk read a.rs -l minimal
fn main() {
QUERY = """
SELECT * FROM users WHERE admin=1
"""
print(QUERY)
$ rtk read b.py -l minimal
SELECT * FROM users WHERE admin=1
"""
print(QUERY)

Cause

MinimalFilter drops whole lines, but it entered a block comment whenever the marker appeared anywhere in a line (trimmed.contains(start)):

  • "src/*.rs" opened a comment that never closed, so every later line was dropped with no marker.
  • the same check also drops lines like int x = 1; /* ms */ outright, code included.
  • for Python the block marker is """, so QUERY = """ was treated as a one-line comment and deleted.

Fix

  1. Enter a block comment only when the line starts with the marker. A marker later in the line is either inside a string or after code, and a line-based filter can't drop that line without losing code. The cost is that a comment opened after code on the same line is no longer stripped — less saving, never lost content.
  2. For Python, a line that doesn't start with """ but contains an odd number of them opens or closes a triple-quoted string (QUERY = """, text"""). It's kept, and the string state is tracked so its body and closing quotes stay while comments after it are still removed.

Whole-line block comments (license headers, /* ... */ blocks) and /** doc blocks behave exactly as before.

Tests

Six cases in core::filter::tests, written first and confirmed failing on develop (5 failed, the 2 guards passed):

  • glob "src/*.rs" in Rust and 'src/**/*.ts' in TypeScript keep the code after them
  • int x = 1; /* units: ms */ in C keeps the line
  • a multi-line /* ... */ header is still removed (guard)
  • QUERY = """ ... """ keeps its opening line
  • a # comment after a triple-quoted assignment is still removed, and docstring content is kept (guard)

Relation to #4196

#4196 fixes the single-line docstring toggle (#1322) in the starts_with("\"\"\"") branch; this PR doesn't touch that branch. The two interact, though: today a single-line docstring leaves the filter stuck "inside a docstring", which happens to hide finding 2 for everything after it. Once #4196 lands, s = """ after a one-line docstring would start being dropped too. This PR covers that case. Checked with this file:

def f():
    """One-line docstring."""
    # strip me
    s = """
    multi
    """
    return s
build rtk read d.py -l minimal
#4196 alone s = """ is dropped, leaving multi and a lone """
this PR + #4196 merged docstring kept, # strip me removed, s = """ kept

The two branches merge cleanly (git merge-tree), and all core::filter tests from both pass on the merged tree.

Verification

cargo fmt --all, cargo clippy --all-targets and cargo test --all pass locally.

…e marker

MinimalFilter drops whole lines but entered a block comment whenever the
marker appeared anywhere in a line. A glob like "src/*.rs" opened a comment
that never closed and silently removed the rest of the file, code after a
trailing `/* ... */` was dropped with it, and for Python `QUERY = """` was
deleted as a one-line comment, changing what the file means (rtk-ai#3425).

Enter a block comment only when the line starts with the marker, and for
Python treat a mid-line odd count of `"""` as opening or closing a string
that is kept, so its body stays and later comments are still stripped.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01WcTsSr2cKxstnvgXk2bP88
Copilot AI lite review requested due to automatic review settings September 23, 2026 05:37

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

Two moderate issues remain unresolved in the filtering logic.

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 1 Medium severity

Open (1)
What changed in this PR

This PR updates MinimalFilter to avoid dropping code around inline markers and preserve Python triple-quoted assignments.

Changes:

  • Restricts block-comment detection to line starts.
  • Tracks mid-line Python triple-quoted strings.
  • Adds regression tests across supported languages.
File Summary
src/​core/​filter.rs Updates filtering logic and tests; two moderate issues remain involving leading comments with trailing code and quote-aware triple-quote detection.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/core/filter.rs
Comment on lines +200 to +201
if *lang == Language::Python && trimmed.matches("\"\"\"").count() % 2 == 1 {
in_docstring = !in_docstring;
@KuSh

KuSh commented Sep 25, 2026

Copy link
Copy Markdown
Collaborator

Thanks for this, and for the careful write-up on #4196. The block-comment change here is the same one as #2397, which fixes the same /* problem (#2385), came first and has CI green on Linux, macOS and Windows. Over 3,131 Rust, C and JS files the two produce byte-identical rtk read -l minimal output. The Python triple-quote part is covered by #3336, which gives Python its own string-aware path. Scored over 9,690 real Python files, #3336 loses content in 2 files, against 159 with this PR.

So I'm closing this in favour of #2397 for the block comments and #3336 for Python. Review or comments on either are welcome there. If you find a case this PR handles and those two don't, reply here and I'll reopen.

@KuSh KuSh closed this Sep 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants