@@ -16,7 +16,7 @@ import type { ReadFileParams, ReadFileMode, ReadFileToolParams, FileEntry, LineR
1616import { isLegacyReadFileParams , type ClineSayTool } from "@roo-code/types"
1717
1818import { Task } from "../task/Task"
19- import { computeVersionToken } from "../../utils/versionToken"
19+ import { versionTokenOfStat } from "../../utils/versionToken"
2020import { formatResponse } from "../prompts/responses"
2121import { RecordSource } from "../context-tracking/FileContextTrackerTypes"
2222import { isPathOutsideWorkspace } from "../../utils/pathUtils"
@@ -215,16 +215,28 @@ export class ReadFileTool extends BaseTool<"read_file"> {
215215 // Read text file content with lossy UTF-8 conversion
216216 // Reading as Buffer first allows graceful handling of non-UTF8 bytes
217217 // (they become U+FFFD replacement characters instead of throwing)
218+ // A2 (epic #1375): capture the on-disk token before the read so a mutation
219+ // landing mid-read is detected by the post-read stat below.
220+ const preReadStats = await fs . stat ( fullPath , { bigint : true } ) . catch ( ( ) => undefined )
218221 const buffer = await fs . readFile ( fullPath )
219222 const fileContent = buffer . toString ( "utf-8" )
220223 const result = this . processTextFile ( fileContent , entry )
221224
222225 await task . fileContextTracker . trackFileContext ( relPath , "read_tool" as RecordSource )
223226
224227 // A2 (plan #33 / epic #1375): record the observed on-disk version for the future write guard.
225- // A stat failure leaves the target unobserved and never fails the read.
226- const version = await computeVersionToken ( fullPath ) . catch ( ( ) => undefined )
227- if ( version ) task . observationRegistry . observe ( fullPath , version )
228+ // The token is captured before AND after the read; the target is observed only
229+ // when both match — a mutation between the two stats means the content the model
230+ // received is not the on-disk state, and observing it would let a later write
231+ // match a token the model never saw. A stat failure leaves the target
232+ // unobserved and never fails the read.
233+ const postReadStats = await fs . stat ( fullPath , { bigint : true } ) . catch ( ( ) => undefined )
234+ if ( preReadStats && postReadStats ) {
235+ const preReadToken = versionTokenOfStat ( preReadStats )
236+ if ( preReadToken === versionTokenOfStat ( postReadStats ) ) {
237+ task . observationRegistry . observe ( fullPath , preReadToken )
238+ }
239+ }
228240
229241 updateFileResult ( relPath , {
230242 nativeContent : `File: ${ relPath } \n${ result } ` ,
@@ -774,6 +786,9 @@ export class ReadFileTool extends BaseTool<"read_file"> {
774786 }
775787
776788 // Read text file
789+ // A2 (epic #1375): capture the on-disk token before the read so a mutation
790+ // landing mid-read is detected by the post-read stat below.
791+ const preReadStats = await fs . stat ( fullPath , { bigint : true } ) . catch ( ( ) => undefined )
777792 const rawContent = await fs . readFile ( fullPath , "utf8" )
778793
779794 // Handle line ranges if specified
@@ -808,9 +823,16 @@ export class ReadFileTool extends BaseTool<"read_file"> {
808823
809824 // A2 (plan #33 / epic #1375): mirror the native path — record the observed
810825 // on-disk version so legacy-format reads also feed the future write guard.
811- // A stat failure leaves the target unobserved and never fails the read.
812- const version = await computeVersionToken ( fullPath ) . catch ( ( ) => undefined )
813- if ( version ) task . observationRegistry . observe ( fullPath , version )
826+ // Observe only when the pre-read and post-read tokens match (a mutation between
827+ // them means the returned content is not the on-disk state). A stat failure
828+ // leaves the target unobserved and never fails the read.
829+ const postReadStats = await fs . stat ( fullPath , { bigint : true } ) . catch ( ( ) => undefined )
830+ if ( preReadStats && postReadStats ) {
831+ const preReadToken = versionTokenOfStat ( preReadStats )
832+ if ( preReadToken === versionTokenOfStat ( postReadStats ) ) {
833+ task . observationRegistry . observe ( fullPath , preReadToken )
834+ }
835+ }
814836 } catch ( error ) {
815837 const errorMsg = error instanceof Error ? error . message : String ( error )
816838 results . push ( `File: ${ relPath } \nError: ${ errorMsg } ` )
0 commit comments