@@ -17,13 +17,14 @@ import type { ReadFileParams, ReadFileMode, ReadFileToolParams, FileEntry, LineR
1717import { isLegacyReadFileParams , type ClineSayTool } from "@roo-code/types"
1818
1919import { Task } from "../task/Task"
20+ import { versionTokenOfStat } from "../../utils/versionToken"
2021import { formatResponse } from "../prompts/responses"
2122import { RecordSource } from "../context-tracking/FileContextTrackerTypes"
2223import { isPathOutsideWorkspace } from "../../utils/pathUtils"
2324import { getReadablePath } from "../../utils/path"
2425import { extractTextFromFile , addLineNumbers , getSupportedBinaryFormats } from "../../integrations/misc/extract-text"
2526import { readWithIndentation , readWithSlice } from "../../integrations/misc/indentation-reader"
26- import { DEFAULT_LINE_LIMIT } from "../prompts/tools/native-tools/read_file"
27+ import { DEFAULT_LINE_LIMIT , MAX_LINE_LENGTH } from "../prompts/tools/native-tools/read_file"
2728import type { ToolUse , PushToolResult } from "../../shared/tools"
2829
2930import {
@@ -215,14 +216,49 @@ export class ReadFileTool extends BaseTool<"read_file"> {
215216 // Read text file content with lossy UTF-8 conversion
216217 // Reading as Buffer first allows graceful handling of non-UTF8 bytes
217218 // (they become U+FFFD replacement characters instead of throwing)
219+ // A2 (epic #1375): capture the on-disk token before the read so a mutation
220+ // landing mid-read is detected by the post-read stat below.
221+ const preReadStats = await fs . stat ( fullPath , { bigint : true } ) . catch ( ( ) => undefined )
218222 const buffer = await fs . readFile ( fullPath )
219223 const fileContent = buffer . toString ( "utf-8" )
220- const result = this . processTextFile ( fileContent , entry )
224+ // A lossy decode is not the whole file: the model never saw those bytes.
225+ const lossyDecode = ! Buffer . from ( fileContent ) . equals ( buffer )
226+ // S4b follow-up (#46 / epic #1375): processTextFile reports whether the
227+ // returned content is the whole file; the observation below records that
228+ // scope so the write guard can deny full-file updates built on a partial view.
229+ const processed = this . processTextFile ( fileContent , entry )
221230
222231 await task . fileContextTracker . trackFileContext ( relPath , "read_tool" as RecordSource )
223232
233+ // A2 (plan #33 / epic #1375): record the observed on-disk version for the future write guard.
234+ // The token is captured before AND after the read; the target is observed only
235+ // when both match — a mutation between the two stats means the content the model
236+ // received is not the on-disk state, and observing it would let a later write
237+ // match a token the model never saw. A stat failure leaves the target
238+ // unobserved and never fails the read.
239+ // A task that was cancelled or disposed while this read was awaiting I/O must
240+ // not record an observation: disposeOnce() has retired the registry, and the
241+ // content this turn produced will never be acted on. Skipping also drops the
242+ // post-read stat work for a task that no longer has a consumer.
243+ if ( ! task . abort ) {
244+ const postReadStats = await fs . stat ( fullPath , { bigint : true } ) . catch ( ( ) => undefined )
245+ // Re-checked after the await: the guard above cannot see a cancellation
246+ // that lands while the stat is in flight, and that read belongs to a run
247+ // that will never act on it.
248+ if ( preReadStats && postReadStats && ! task . abort ) {
249+ const preReadToken = versionTokenOfStat ( preReadStats )
250+ if ( preReadToken === versionTokenOfStat ( postReadStats ) ) {
251+ task . observationRegistry . observe (
252+ fullPath ,
253+ preReadToken ,
254+ processed . complete && ! lossyDecode ,
255+ )
256+ }
257+ }
258+ }
259+
224260 updateFileResult ( relPath , {
225- nativeContent : `File: ${ relPath } \n${ result } ` ,
261+ nativeContent : `File: ${ relPath } \n${ processed . content } ` ,
226262 } )
227263 } catch ( error ) {
228264 const errorMsg = error instanceof Error ? error . message : String ( error )
@@ -266,8 +302,14 @@ export class ReadFileTool extends BaseTool<"read_file"> {
266302
267303 /**
268304 * Process a text file according to the requested mode.
305+ *
306+ * Returns the content string plus whether that content is the complete
307+ * file (S4b follow-up #46 / epic #1375): slice mode is complete only
308+ * when it starts at line 1, returns every line, and was not truncated;
309+ * indentation mode is never complete because it returns semantic blocks
310+ * of the file, not the file itself.
269311 */
270- private processTextFile ( content : string , entry : InternalFileEntry ) : string {
312+ private processTextFile ( content : string , entry : InternalFileEntry ) : { content : string ; complete : boolean } {
271313 const mode = entry . mode || "slice"
272314
273315 if ( mode === "indentation" ) {
@@ -300,7 +342,15 @@ export class ReadFileTool extends BaseTool<"read_file"> {
300342 output += `\n\nIncluded ranges: ${ rangeStr } (total: ${ result . totalLines } lines)`
301343 }
302344
303- return output
345+ if ( result . hasClippedLines ) {
346+ // A returned line can still be a partial view: clipping is reported
347+ // separately from the omission notice, because the block may show
348+ // every line it includes in truncated form.
349+ output += `\nNote: Some lines in this view exceed ${ MAX_LINE_LENGTH } characters and were clipped in this view.`
350+ }
351+
352+ // Indentation mode returns semantic blocks: never a complete file view.
353+ return { content : output , complete : false }
304354 }
305355
306356 // Slice mode (default): simple offset/limit reading
@@ -323,11 +373,27 @@ export class ReadFileTool extends BaseTool<"read_file"> {
323373 To read more: Use the read_file tool with offset=${ nextOffset } and limit=${ limit } .
324374
325375 ${ result . content } `
376+ if ( result . hasClippedLines ) {
377+ // The slice cut lines off and also clipped long lines inside it, so both
378+ // notices belong to the response.
379+ output += `\nNote: Some lines in this view exceed ${ MAX_LINE_LENGTH } characters and were clipped in this view.`
380+ }
381+ } else if ( result . hasClippedLines ) {
382+ // Every line was returned, so there is no later offset to read: report the
383+ // clipping without a next-offset hint, and keep the read incomplete so a
384+ // full-file replacement cannot be built from a clipped line.
385+ output = `IMPORTANT: Some lines exceed ${ MAX_LINE_LENGTH } characters and were clipped in this view. ${ offset1 === 1 ? "The file was read in full" : `The returned slice starts at line ${ offset1 } and reaches the end of the file` } , but the clipped lines were not shown in full.\n${ result . content } `
326386 } else if ( result . returnedLines === 0 && result . totalLines === 0 ) {
327387 output = "Note: File is empty"
328388 }
329389
330- return output
390+ // Complete only when the slice starts at line 1, returned every line, and
391+ // showed every line in full (returnedLines === totalLines follows from the
392+ // first two conditions): a partial start, a truncated tail, or a clipped
393+ // line means the model did not see the whole file.
394+ const complete = offset0 === 0 && ! result . wasTruncated && ! result . hasClippedLines
395+
396+ return { content : output , complete }
331397 }
332398
333399 /**
@@ -770,9 +836,20 @@ export class ReadFileTool extends BaseTool<"read_file"> {
770836 }
771837
772838 // Read text file
773- const rawContent = await fs . readFile ( fullPath , "utf8" )
839+ // A2 (epic #1375): capture the on-disk token before the read so a mutation
840+ // landing mid-read is detected by the post-read stat below.
841+ const preReadStats = await fs . stat ( fullPath , { bigint : true } ) . catch ( ( ) => undefined )
842+ const rawBuffer = await fs . readFile ( fullPath )
843+ const rawContent = rawBuffer . toString ( "utf-8" )
844+ // Same contract: a lossy decode is a partial view.
845+ const lossyDecode = ! Buffer . from ( rawContent ) . equals ( rawBuffer )
774846
775847 // Handle line ranges if specified
848+ // S4b follow-up (#46 / epic #1375): a line-range read returns only the requested
849+ // ranges, and a slice truncated to DEFAULT_LINE_LIMIT returns only the head of
850+ // the file — record such observations as partial so the write guard denies a
851+ // full-file update built on them.
852+ let readComplete = false
776853 let content : string
777854 if ( entry . lineRanges && entry . lineRanges . length > 0 ) {
778855 const lines = rawContent . split ( "\n" )
@@ -792,15 +869,41 @@ export class ReadFileTool extends BaseTool<"read_file"> {
792869 // Read with default limits using slice mode
793870 const result = readWithSlice ( rawContent , 0 , DEFAULT_LINE_LIMIT )
794871 content = result . content
872+ readComplete = ! result . wasTruncated && ! result . hasClippedLines
795873 if ( result . wasTruncated ) {
796874 content += `\n\n[File truncated: showing ${ result . returnedLines } of ${ result . totalLines } total lines]`
875+ if ( result . hasClippedLines ) {
876+ // Both notices: the slice was truncated and a line inside it was
877+ // clipped.
878+ content += `\n\n[Some lines exceed the per-line length cap and were clipped in this view]`
879+ }
880+ } else if ( result . hasClippedLines ) {
881+ content += `\n\n[Some lines exceed the per-line length cap and were clipped in this view]`
797882 }
798883 }
799884
800885 results . push ( `File: ${ relPath } \n${ content } ` )
801886
802887 // Track file in context
803888 await task . fileContextTracker . trackFileContext ( relPath , "read_tool" )
889+
890+ // A2 (plan #33 / epic #1375): mirror the native path — record the observed
891+ // on-disk version so legacy-format reads also feed the future write guard.
892+ // Observe only when the pre-read and post-read tokens match (a mutation between
893+ // them means the returned content is not the on-disk state). A stat failure
894+ // leaves the target unobserved and never fails the read.
895+ // Same contract as the native path: an aborted or disposed task records nothing.
896+ if ( ! task . abort ) {
897+ const postReadStats = await fs . stat ( fullPath , { bigint : true } ) . catch ( ( ) => undefined )
898+ // Re-checked after the await, as on the native path: a cancel landing
899+ // during the stat must still record nothing.
900+ if ( preReadStats && postReadStats && ! task . abort ) {
901+ const preReadToken = versionTokenOfStat ( preReadStats )
902+ if ( preReadToken === versionTokenOfStat ( postReadStats ) ) {
903+ task . observationRegistry . observe ( fullPath , preReadToken , readComplete && ! lossyDecode )
904+ }
905+ }
906+ }
804907 } catch ( error ) {
805908 const errorMsg = error instanceof Error ? error . message : String ( error )
806909 results . push ( `File: ${ relPath } \nError: ${ errorMsg } ` )
0 commit comments