1. Comments
The comments blade reads every comment in your source files and sorts it into one of six classes. Four classes are noise that CodeKnife can delete for you. One class (workaround and apology comments) is flagged for you to fix by hand. Everything else is kept. The blade also reports inline lint and type suppressions such as @ts-ignore, # noqa and #[allow(...)].
Use it to clean up commented-out code, section banners and conversational leftovers before review, and to keep suppressions visible.
Quick example
Section titled “Quick example”This small repository has a Rust, a Python and a TypeScript file:
// SPDX-License-Identifier: MIT// ========= HELPERS =========
/// Adds two numbers.pub fn add(a: i32, b: i32) -> i32 { // Step 1: compute the sum let sum = a + b; // let old = a * b; // workaround: keep this for now sum}
/// Returns the total.pub fn total(items: &[i32]) -> i32 { let mut total = 0; // loop through items for i in items { total += i; // increment total } // See https://example.com/spec for the rounding rule total}
#[allow(dead_code)]fn unused_helper() {}# -*- coding: utf-8 -*-import os # noqa: F401
def fetch(url): # check if url is empty if not url: return None # TODO(#42): add retries x = len(url) # type: ignore return x// ------------------------------// eslint-disable-next-line no-consoleconsole.log("boot");
export function load(id: string): string { // @ts-ignore const n: number = id; return String(n);}Run the blade:
codeknife comments --format humaninfo[CK_CMT_NARRATION]: strippable comment: narration restating adjacent code --> py/client.py:6 (fetch) = fix: Run `codeknife comments --strip --write` to remove safely.
info[CK_CMT_SUPPRESSION]: MUST-RESHAPE: `noqa` lint/type suppression --> py/client.py:2 = fix: Fix the underlying type or lint warning instead of suppressing it.
warning[CK_CMT_SUPPRESSION]: MUST-RESHAPE: `type: ignore` lint/type suppression --> py/client.py:10 (fetch) = fix: Fix the underlying type or lint warning instead of suppressing it.
info[CK_CMT_BANNER]: strippable comment: section divider banner --> src/lib.rs:2 = fix: Run `codeknife comments --strip --write` to remove safely.
info[CK_CMT_AI_RESIDUE]: strippable comment: conversational AI residue or trivial boilerplate --> src/lib.rs:6 (add) = fix: Run `codeknife comments --strip --write` to remove safely.
warning[CK_CMT_CORPSE]: strippable comment: commented-out dead code corpse --> src/lib.rs:8 (add) = fix: Run `codeknife comments --strip --write` to remove safely.
warning[CK_CMT_APOLOGY]: MUST-RESHAPE: workaround/apology comment found --> src/lib.rs:9 (add) = fix: Refactor the underlying function/type or encode this constraint as a type/test, then delete the comment.
info[CK_CMT_NARRATION]: strippable comment: narration restating adjacent code --> src/lib.rs:16 (total) = fix: Run `codeknife comments --strip --write` to remove safely.
info[CK_CMT_NARRATION]: strippable comment: narration restating adjacent code --> src/lib.rs:18 (total) = fix: Run `codeknife comments --strip --write` to remove safely.
warning[CK_CMT_SUPPRESSION]: MUST-RESHAPE: `#[allow(...)]` lint/type suppression --> src/lib.rs:24 = fix: Fix the underlying type or lint warning instead of suppressing it.
info[CK_CMT_BANNER]: strippable comment: section divider banner --> web/app.ts:1 = fix: Run `codeknife comments --strip --write` to remove safely.
info[CK_CMT_SUPPRESSION]: MUST-RESHAPE: `eslint-disable` lint/type suppression --> web/app.ts:2 = fix: Fix the underlying type or lint warning instead of suppressing it.
warning[CK_CMT_SUPPRESSION]: MUST-RESHAPE: `@ts-ignore` lint/type suppression --> web/app.ts:6 (load) = fix: Fix the underlying type or lint warning instead of suppressing it.
codeknife comments: FAIL (3 files, 0 errors, 5 warnings, 8 info, 2ms)The licence header, both doc comments, the coding line, the TODO(#42) line and the comment with a URL produce no finding. They are on the keep-list (see below).
Preview the strip
Section titled “Preview the strip”--strip on its own is a dry run. It prints the same findings plus a note with the number of comments it would remove. No file changes.
codeknife comments --strip --format human...note: dry-run: 7 strippable comment(s) detected; pass `--write` to apply editscodeknife comments: FAIL (3 files, 0 errors, 5 warnings, 8 info, 3ms)Apply the strip
Section titled “Apply the strip”Add --write to edit the files:
codeknife comments --strip --write --format human...note: applied verified comment stripping to 3 file(s)codeknife comments: FAIL (3 files, 0 errors, 5 warnings, 8 info, 5ms)The resulting git diff:
diff --git a/py/client.py b/py/client.pyindex 2d6e4ce..e663cd1 100644--- a/py/client.py+++ b/py/client.py@@ -3,7 +3,6 @@ import os # noqa: F401
def fetch(url):- # check if url is empty if not url: return None # TODO(#42): add retriesdiff --git a/src/lib.rs b/src/lib.rsindex f972ebb..2709db4 100644--- a/src/lib.rs+++ b/src/lib.rs@@ -1,11 +1,8 @@ // SPDX-License-Identifier: MIT-// ========= HELPERS =========
/// Adds two numbers. pub fn add(a: i32, b: i32) -> i32 {- // Step 1: compute the sum let sum = a + b;- // let old = a * b; // workaround: keep this for now sum }@@ -13,9 +10,8 @@ pub fn add(a: i32, b: i32) -> i32 { /// Returns the total. pub fn total(items: &[i32]) -> i32 { let mut total = 0;- // loop through items for i in items {- total += i; // increment total+ total += i; } // See https://example.com/spec for the rounding rule totaldiff --git a/web/app.ts b/web/app.tsindex 326002e..1fc88da 100644--- a/web/app.ts+++ b/web/app.ts@@ -1,4 +1,3 @@-// ------------------------------ // eslint-disable-next-line no-console console.log("boot");The workaround comment and all suppressions stay. You fix those yourself.
Comment classes
Section titled “Comment classes”Every comment gets exactly one class. The checks run in this order and the first match wins:
- Keep-list
- Apology sermon
- Banner
- Corpse (commented-out code)
- AI residue
- Narration
- Otherwise: keep
Before matching, CodeKnife cleans the comment text. It removes /* and */ and leading * from block comments, and the prefixes ///, //!, // and # (plus one following space) from line comments. Most checks compare against the lowercased, cleaned text.
| Class | Rule ID | Severity | Strippable |
|---|---|---|---|
| Keep | none | none | no |
| Apology sermon | CK_CMT_APOLOGY |
warning | no |
| Banner | CK_CMT_BANNER |
info | yes |
| Corpse | CK_CMT_CORPSE |
warning | yes |
| AI residue | CK_CMT_AI_RESIDUE |
info | yes |
| Narration | CK_CMT_NARRATION |
info | yes |
Keep-list
Section titled “Keep-list”A comment is kept, with no finding, if any of these is true:
- The raw comment text starts with one of
//go:,// +build,//!,#!,# type:,# coding:or# -*-. - The lowercased text contains a licence marker:
spdx-license-identifier,copyright (c),licensed under,mit licenseorapache license. - The lowercased text contains a tool directive:
@jsx,prettier-ignore,rustfmt::skip,fmt: off,fmt: on,fmt: skiporcodegen. - The comment sits directly above an
unsafeblock (the next syntax node is anunsafe_block, or its text starts withunsafe). - The cleaned text starts with
safety:(any case). - The text contains
http://orhttps://. - The text contains an issue reference: a
#followed directly by a digit, for example#42. - The text contains one of the suppression markers listed under Suppressions.
- It is a doc comment (see below) and doc comments are protected.
A comment is a doc comment if it starts with ///, //! or /**. Doc comments are kept when keep_pub_docs = true (the default) and you do not pass --strip-pub-docs. One exception: a doc comment that is a trivial echo of the next line (see AI residue) is not protected.
Plain TODO and FIXME comments are not flagged by any rule. They fall through to the default keep class.
Apology sermon (CK_CMT_APOLOGY)
Section titled “Apology sermon (CK_CMT_APOLOGY)”The lowercased text contains any of: do not remove, do not touch, don't remove, don't touch, workaround, for now, too risky, hack:, kludge, important:.
It also matches a comment inside a function body that spans more lines than max_inline_prose_lines (default 4). Consecutive // or # lines are separate comments, so in practice this length check fires on multi-line block comments such as /* ... */. In testing, a five-line /* */ block inside a Rust function was reported as src/lib.rs:5-9.
These comments are never stripped. The message asks you to reshape the code (or encode the constraint in a type or test) and then delete the comment.
Banner (CK_CMT_BANNER)
Section titled “Banner (CK_CMT_BANNER)”Either:
- The cleaned text contains at least 6 of the characters
=,-,*,~,#, and those characters make up at least half of its length. Example:// ========= HELPERS =========. - The whole lowercased text is exactly one of:
helpers,helper functions,utilities,types,constants,imports,exports,getters,setters,constructor,methods,tests,main. Example: a lone// Helpersline.
Corpse (CK_CMT_CORPSE)
Section titled “Corpse (CK_CMT_CORPSE)”A comment is commented-out code when all of these hold:
- The cleaned text is not empty and does not start with three backticks,
>>>or...(Markdown fences and doctest prompts are protected). - It does not start with
e.g.,i.e.,note:,example:,usage:,see,returnsorformat:(any case). - It contains at least one code cue:
;,{,},=>,->,::,==,!=, an=with a space on each side,(),fn,let,const,def,func,returnorimport. - It parses without syntax errors using the tree-sitter grammar of the file’s own language.
- The parse tree has at least 3 nodes, and at least one of them is a statement-like node:
function_item,function_declaration,function_definition,let_declaration,lexical_declaration,assignment,return_statement,return_expression,call_expression,call,if_expression,if_statement,import_statement,use_declaration,struct_item,class_declarationorexpression_statement.
Corpses are the only strippable class reported as a warning.
AI residue (CK_CMT_AI_RESIDUE)
Section titled “AI residue (CK_CMT_AI_RESIDUE)”Any of:
- The lowercased text contains
added as requested,updated to handle,step 1:,step 2:,step 3:,step 4:,initialize variablesorreturn the result. - The comment is inside a function body and starts with
this helperorthis function. - It is a trivial doc echo: a single-line doc comment with 2 to 5 content words, every one of which also appears in the first two lines of the next syntax node. For example
/** Formats the name. */aboveexport function formatName(...).
Narration (CK_CMT_NARRATION)
Section titled “Narration (CK_CMT_NARRATION)”Only comments inside a function body, or trailing comments after code on the same line, can be narration. Then either:
- The lowercased text starts with
increment,decrement,loop through,iterate over,check if,create a new,set theorreturn. - The comment has 2 to 14 content words, the next syntax node has at least one, and the share of comment words that also appear in the next node’s first two lines is at least
narration_overlap_threshold(default0.65).
Content words are built like this: split on any non-alphanumeric character, split camelCase into parts, lowercase, strip one suffix of ing, ed, es or s when the word is long enough, then drop words shorter than 2 characters and these stop words: the a an to of for in on with and or is are be this that from into by as fn def func let const var pub return async.
Prose blocks
Section titled “Prose blocks”A run of plain // or # lines is treated as one unit. If a corpse, narration or AI-residue line sits directly above or below a kept plain line comment (no gap, neither is a trailing comment), it is also kept. This repeats until nothing changes. So this Python block produces no finding, even though the last line looks like a doctest:
# Lazily import json. # There is no need to import it eagerly. # >>> handler(1)Banners and apology comments are not protected by this rule.
Suppressions
Section titled “Suppressions”The blade also reports CK_CMT_SUPPRESSION for every inline lint or type suppression it finds.
Comment markers. For every line that a comment covers, CodeKnife checks the whole source line for these substrings (first match wins):
| Marker | Kind | Severity |
|---|---|---|
@ts-ignore |
safety | warning |
@ts-nocheck |
safety | warning |
@ts-expect-error |
safety | warning |
eslint-disable |
style | info |
type: ignore |
safety | warning |
pyright: ignore |
safety | warning |
noqa |
style | info |
pylint: disable |
style | info |
nolint |
style | info |
Rust attributes. Any source line whose trimmed text starts with #[allow( or #![allow( is reported with marker #[allow(...)]. It is a safety suppression (warning) when the line contains warnings, clippy::all, dead_code or unused, and a style suppression (info) otherwise.
Exception: @ts-expect-error in a test file is not reported. Test files are detected per language (for TypeScript and JavaScript, paths containing __tests__/, .test. or .spec., among others). See Language Support.
Suppressions are never stripped. Comments that contain a marker are on the keep-list, so --strip leaves them alone.
The age and expiry rules for suppressions ([suppressions] in codeknife.toml) belong to the Loosening Guard, not to this blade.
How stripping works
Section titled “How stripping works”Only the four strippable classes are removed: narration, banner, corpse and AI residue.
- A comment that is alone on its line (only spaces or tabs around it) is removed together with its whole line and line break.
- A trailing comment after code is removed together with the spaces and tabs before it. The code stays.
- Overlapping removals are merged.
- CodeKnife applies all edits for a file in memory, then re-parses the result with tree-sitter. If the parser fails or the new tree has any syntax error, the file is not written and a note is added:
note: bad.ts: stripping comments produced syntax errors; aborted writenote: applied verified comment stripping to 0 file(s)This also means a file that already has syntax errors is never stripped.
Without --strip, --write does nothing. Stripping needs both flags.
| Flag | Effect |
|---|---|
--strip |
Plan removal of strippable comments. Without --write this only adds the dry-run note. |
--write |
With --strip, write the edited files. Ignored without --strip. |
--strip-pub-docs |
Stop protecting doc comments (///, /**). They are then classified like any other comment. |
Global flags such as --path, --format, --changed, --since, --no-cache and --max-items work as described in the CLI Reference. With --changed, only files in the changed set are analysed and stripped.
--strict has no effect on this blade.
Configuration
Section titled “Configuration”Keys in the [comments] table of codeknife.toml:
| Key | Default | Effect |
|---|---|---|
keep_pub_docs |
true |
Protect doc comments. Set to false to classify them like other comments (same effect as --strip-pub-docs). |
narration_overlap_threshold |
0.65 |
Minimum share of comment words that must appear in the next line for overlap-based narration. |
max_inline_prose_lines |
4 |
A comment inside a function body longer than this many lines is an apology sermon. |
Example with all three changed:
[comments]max_inline_prose_lines = 10narration_overlap_threshold = 1.5keep_pub_docs = falseOn a test repository this removed the long-block apology finding and the overlap-based narration finding, and added a banner finding for a /// ============== doc comment.
A value of the wrong type stops the run:
codeknife error: parsing codeknife.toml: TOML parse error at line 2, column 26 |2 | max_inline_prose_lines = "four" | ^^^^^^invalid type: string "four", expected usizeThe [blades] switch comments = false turns the blade off inside codeknife check. It does not affect codeknife comments itself. See codeknife.toml.
Languages
Section titled “Languages”Comments come from the tree-sitter parse of each source file. CodeKnife collects nodes of kind comment, line_comment and block_comment, which covers Rust (//, ///, //!, /* */), TypeScript, TSX and JavaScript (//, /* */, /** */), Go (//, /* */) and Python (#). See Language Support for which files are parsed.
Python docstrings are string literals, not comments, so this blade never sees them.
Test files are analysed like any other file. Only the @ts-expect-error exception above is test-aware.
| Rule ID | Severity | Trigger | Controlled by | --strict |
Fixer |
|---|---|---|---|---|---|
CK_CMT_NARRATION |
info | Narration starter or word overlap | narration_overlap_threshold |
no change | yes, CK_CMT_STRIP |
CK_CMT_BANNER |
info | Divider punctuation or section-name comment | none | no change | yes, CK_CMT_STRIP |
CK_CMT_CORPSE |
warning | Comment parses as code | none | no change | yes, CK_CMT_STRIP |
CK_CMT_AI_RESIDUE |
info | AI phrases, this function ..., trivial doc echo |
none | no change | yes, CK_CMT_STRIP |
CK_CMT_APOLOGY |
warning | Workaround wording or long comment in a function | max_inline_prose_lines |
no change | no |
CK_CMT_SUPPRESSION |
warning (safety) or info (style) | Suppression marker or #[allow(...)] |
none | no change | no |
All strippable findings carry an impact score of 25 and include the comment’s byte range in location. Apology findings score 65, safety suppressions 70 and style suppressions 35.
Autofix
Section titled “Autofix”codeknife fix has one comment fixer. It runs the same classifier (as if --strip-pub-docs were not passed; keep_pub_docs still applies) and the same strip planner, and emits one patch per file under the rule ID CK_CMT_STRIP:
codeknife fix (dry-run): planned 4 patch(es) across 3 file(s), 0 reverted (1 pass(es)) [FIX] CK_CMT_STRIP | Remove 1 strippable comment(s) in py/client.py [FIX] CK_CMT_STRIP | Remove 5 strippable comment(s) in src/lib.rs [FIX] CK_CMT_STRIP | Remove 1 strippable comment(s) in web/app.ts [FIX] CK_DEAD_SYMBOL | Remove unreachable private function in src/lib.rsNo fixer exists for CK_CMT_APOLOGY or CK_CMT_SUPPRESSION. Expired suppressions are handled by the guard’s fixer; see Loosening Guard.
The full list of rule IDs across all blades is in the Rule Catalogue.
Exit codes
Section titled “Exit codes”| Code | Meaning |
|---|---|
0 |
No error or warning findings remain after the baseline is applied. Info findings alone pass. |
1 |
At least one error or warning finding. |
2 |
CodeKnife failed, for example an invalid codeknife.toml. |
Findings already recorded in codeknife.baseline are filtered out first. See Ratchet Baseline and Output Formats & Exit Codes.
Also available via
Section titled “Also available via”codeknife checkruns this blade (classification and suppressions only, never stripping) when[blades] comments = true.- The MCP tool
codeknife_checkruns the same suite, andcodeknife_fixruns theCK_CMT_STRIPfixer. See MCP Tools.