Skip to content

fix: count only non-comment lines in command-sequence detection - #2175

Open
Osamaali313 wants to merge 1 commit into
stackblitz-labs:mainfrom
Osamaali313:fix/command-sequence-comment-ratio
Open

fix: count only non-comment lines in command-sequence detection#2175
Osamaali313 wants to merge 1 commit into
stackblitz-labs:mainfrom
Osamaali313:fix/command-sequence-comment-ratio

Conversation

@Osamaali313

Copy link
Copy Markdown

What

EnhancedStreamingMessageParser._isCommandSequence decides whether a fenced code block is a shell command sequence (to execute) or a file (to write), by what fraction of lines look like commands. The numerator already excludes comment/empty lines, but the denominator used lines.length (every line):

const commandLikeLines = lines.filter(
  (line) => line.length > 0 && !line.startsWith('#') && (this._isSingleLineCommand(line) || this._isSimpleCommand(line)),
);
// If more than 70% of non-comment lines are commands, treat as command sequence
return commandLikeLines.length / lines.length > 0.7;

The comment says "70% of non-comment lines", but the math divides by all lines. So comment lines dilute the ratio. A normal command snippet with explanatory comments:

```bash
# Install dependencies
npm install
# Start the dev server
npm run dev
```

is 2/2 = 100% commands by the intended rule, but computes 2/4 = 50%, falls below the 0.7 threshold, and is misclassified as a file to write instead of shell commands to run — the exact "code written to files instead of executed" failure this enhanced parser was built to address.

Fix

Compute the ratio over non-comment, non-empty lines (matching the comment/intent), and guard the all-comment case against 0/0 = NaN:

const nonCommentLines = lines.filter((line) => line.length > 0 && !line.startsWith('#'));
if (nonCommentLines.length === 0) {
  return false;
}
const commandLikeLines = nonCommentLines.filter(
  (line) => this._isSingleLineCommand(line) || this._isSimpleCommand(line),
);
return commandLikeLines.length / nonCommentLines.length > 0.7;

Testing

Added a regression test to message-parser.spec.ts (a comment-interleaved bash block is detected as shell). It fails on main (the block isn't recognized → onActionOpen never fires for a shell action) and passes with the fix.

vitest run app/lib/runtime/message-parser.spec.ts
✓ 46 tests passed

pnpm typecheck and pnpm lint pass (pre-commit hook green).

_isCommandSequence classifies a fenced code block as a shell command sequence
(to execute) vs a file (to write). Its numerator filters out comment/empty
lines, but the denominator used lines.length (all lines), so the comment says
"more than 70% of non-comment lines" while the math divides by every line.

A normal command block with explanatory comments, e.g.

    # Install dependencies
    npm install
    # Start the dev server
    npm run dev

is 2/2 = 100% commands by the intended rule but computed 2/4 = 50%, failing the
0.7 threshold and being misclassified as a file to write instead of run. Divide
by the non-comment line count to match the documented behavior, and guard the
all-comment case against a 0/0 = NaN comparison.
Copilot AI review requested due to automatic review settings June 21, 2026 20:11

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 was unable to review this pull request because the user who requested the review has reached their quota limit.

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.

2 participants