-
Notifications
You must be signed in to change notification settings - Fork 16
fix: correct false non-interactive classification for large sessions #277
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mattleaverton
merged 2 commits into
danshapiro:main
from
mattleaverton:fix/noninteractive-scan-fallback
Apr 2, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
148 changes: 148 additions & 0 deletions
148
test/unit/server/coding-cli/scan-user-text-messages.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| // Tests for scanFileForUserTextMessages — byte-level fallback that corrects | ||
| // false non-interactive classification on large truncated session files. | ||
| import { describe, it, expect, beforeEach, afterEach } from 'vitest' | ||
| import path from 'path' | ||
| import os from 'os' | ||
| import fsp from 'fs/promises' | ||
| import { scanFileForUserTextMessages } from '../../../../server/coding-cli/session-indexer.js' | ||
|
|
||
| let tempDir: string | ||
|
|
||
| beforeEach(async () => { | ||
| tempDir = await fsp.mkdtemp(path.join(os.tmpdir(), 'freshell-scan-test-')) | ||
| }) | ||
|
|
||
| afterEach(async () => { | ||
| await fsp.rm(tempDir, { recursive: true, force: true }) | ||
| }) | ||
|
|
||
| function jsonlLine(obj: Record<string, unknown>): string { | ||
| return JSON.stringify(obj) | ||
| } | ||
|
|
||
| function userTextMessage(content: string): string { | ||
| return jsonlLine({ | ||
| type: 'user', | ||
| message: { role: 'user', content }, | ||
| }) | ||
| } | ||
|
|
||
| function userToolResult(): string { | ||
| return jsonlLine({ | ||
| type: 'user', | ||
| message: { role: 'user', content: [{ type: 'tool_result', tool_use_id: 'abc', content: '' }] }, | ||
| }) | ||
| } | ||
|
|
||
| function assistantMessage(text: string): string { | ||
| return jsonlLine({ | ||
| type: 'assistant', | ||
| message: { role: 'assistant', content: [{ type: 'text', text }] }, | ||
| }) | ||
| } | ||
|
|
||
| describe('scanFileForUserTextMessages', () => { | ||
| it('returns false for empty file', async () => { | ||
| const filePath = path.join(tempDir, 'empty.jsonl') | ||
| await fsp.writeFile(filePath, '') | ||
| expect(await scanFileForUserTextMessages(filePath)).toBe(false) | ||
| }) | ||
|
|
||
| it('returns false for single text user message', async () => { | ||
| const filePath = path.join(tempDir, 'single.jsonl') | ||
| await fsp.writeFile(filePath, [ | ||
| userTextMessage('Hello'), | ||
| assistantMessage('Hi there'), | ||
| ].join('\n')) | ||
| expect(await scanFileForUserTextMessages(filePath)).toBe(false) | ||
| }) | ||
|
|
||
| it('returns true for two text user messages', async () => { | ||
| const filePath = path.join(tempDir, 'two.jsonl') | ||
| await fsp.writeFile(filePath, [ | ||
| userTextMessage('Hello'), | ||
| assistantMessage('Hi there'), | ||
| userTextMessage('Do something'), | ||
| assistantMessage('Done'), | ||
| ].join('\n')) | ||
| expect(await scanFileForUserTextMessages(filePath)).toBe(true) | ||
| }) | ||
|
|
||
| it('does not count tool_result user messages toward interactivity', async () => { | ||
| const filePath = path.join(tempDir, 'tool-results.jsonl') | ||
| await fsp.writeFile(filePath, [ | ||
| userTextMessage('Hello'), | ||
| assistantMessage('Let me check'), | ||
| userToolResult(), | ||
| userToolResult(), | ||
| userToolResult(), | ||
| ].join('\n')) | ||
| expect(await scanFileForUserTextMessages(filePath)).toBe(false) | ||
| }) | ||
|
|
||
| it('returns true when text messages are separated by many tool_result messages', async () => { | ||
| const filePath = path.join(tempDir, 'mixed.jsonl') | ||
| const lines = [ | ||
| userTextMessage('First question'), | ||
| assistantMessage('Working on it'), | ||
| ] | ||
| // Add many tool_result exchanges in between | ||
| for (let i = 0; i < 50; i++) { | ||
| lines.push(userToolResult()) | ||
| lines.push(assistantMessage(`Step ${i}`)) | ||
| } | ||
| lines.push(userTextMessage('Second question')) | ||
| lines.push(assistantMessage('Here you go')) | ||
| await fsp.writeFile(filePath, lines.join('\n')) | ||
| expect(await scanFileForUserTextMessages(filePath)).toBe(true) | ||
| }) | ||
|
|
||
| it('handles pattern spanning chunk boundaries', async () => { | ||
| // Create a file where user text messages are separated by enough data | ||
| // to span multiple 64KB chunks | ||
| const filePath = path.join(tempDir, 'large.jsonl') | ||
| const lines = [userTextMessage('First message')] | ||
| // Pad with large assistant messages to push past 64KB | ||
| const bigText = 'x'.repeat(70_000) | ||
| lines.push(assistantMessage(bigText)) | ||
| lines.push(userTextMessage('Second message')) | ||
| await fsp.writeFile(filePath, lines.join('\n')) | ||
| expect(await scanFileForUserTextMessages(filePath)).toBe(true) | ||
| }) | ||
|
|
||
| it('does not double-count a pattern at an exact chunk boundary', async () => { | ||
| // Place a single user text message so the target byte pattern starts at | ||
| // exactly byte 64KB — the overlap position scanned by both adjacent chunks. | ||
| const chunkSize = 64 * 1024 | ||
| const filePath = path.join(tempDir, 'boundary.jsonl') | ||
| const pattern = '"role":"user","content":"' | ||
| // Build a user message line, then figure out where the pattern sits inside it | ||
| const userLine = userTextMessage('Only message') | ||
| const patternOffsetInLine = userLine.indexOf(pattern) | ||
| // Pad so that (padding + \n + patternOffsetInLine) = chunkSize | ||
| const prefixLength = chunkSize - 1 - patternOffsetInLine // -1 for \n separator | ||
| const padding = 'x'.repeat(prefixLength) | ||
| const content = padding + '\n' + userLine | ||
| // Sanity-check alignment | ||
| expect(content.indexOf(pattern)).toBe(chunkSize) | ||
| await fsp.writeFile(filePath, content) | ||
| // Only one text user message — must return false, not double-count | ||
| expect(await scanFileForUserTextMessages(filePath)).toBe(false) | ||
| }) | ||
|
|
||
| it('returns false for nonexistent file', async () => { | ||
| const filePath = path.join(tempDir, 'nonexistent.jsonl') | ||
| expect(await scanFileForUserTextMessages(filePath)).toBe(false) | ||
| }) | ||
|
|
||
| it('returns false for file with only tool_result user messages', async () => { | ||
| const filePath = path.join(tempDir, 'only-tools.jsonl') | ||
| await fsp.writeFile(filePath, [ | ||
| userToolResult(), | ||
| assistantMessage('Done'), | ||
| userToolResult(), | ||
| assistantMessage('Done again'), | ||
| ].join('\n')) | ||
| expect(await scanFileForUserTextMessages(filePath)).toBe(false) | ||
| }) | ||
| }) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The scanner reads
chunkSize + pattern.lengthbytes but advancespositionby onlychunkSize, so bytes at each 64 KiB boundary are scanned twice. If a single"role":"user","content":"sequence starts exactly at one of those boundaries, it is counted once in the previous window and again in the next, causingfound > 1and incorrectly reclassifying a truly non-interactive session as interactive.Useful? React with 👍 / 👎.