@@ -18,6 +18,7 @@ import { arePathsEqual, getReadablePath } from "../../utils/path"
1818import { formatResponse } from "../../core/prompts/responses"
1919import { diagnosticsToProblemsString , getNewDiagnostics } from "../diagnostics"
2020import { Task } from "../../core/task/Task"
21+ import type { FileObservation } from "../../core/task/observationRegistry"
2122import { versionTokenOfStat } from "../../utils/versionToken"
2223import { withFileLock } from "../../utils/fileLock"
2324import { resolveLockKey } from "../../services/file-safety/safeWriteText"
@@ -160,13 +161,15 @@ export class DiffViewProvider {
160161 * caller never read, publishing stale content over the intervening change.
161162 */
162163 private preOpenObservation : { version : string ; complete : boolean } | null | undefined = undefined
163- // The observation this session's preview recorded (path + token), if any.
164- // performFinalReset() withdraws it while the entry still holds that token : the
165- // preview is the tool's own look, not a read the model made, and on the exit
166- // paths that never reach saveChanges' restore (rejected update saves, cancelled
164+ // The entry object this session's preview wrote (path + the exact stored
165+ // object), if any. performFinalReset() withdraws only that object : the preview
166+ // is the tool's own look, not a read the model made, and on the exit paths
167+ // that never reach saveChanges' restore (rejected update saves, cancelled
167168 // tools, reverts) a leftover entry would authorize a later targeted edit for a
168- // file the model never read.
169- private previewRecordedObservation : { path : string ; version : string } | undefined = undefined
169+ // file the model never read. Identity, not version/completeness: a model
170+ // partial read while the diff is open replaces the entry with a fresh object
171+ // at the same token and flag, and that read earned its own authorization.
172+ private previewRecordedObservation : { path : string ; entry : FileObservation } | undefined = undefined
170173 // The on-disk version open() stat-matched, kept so accept-time adoption can
171174 // verify the save was authorized against that exact version.
172175 private openToken : string | undefined = undefined
@@ -266,7 +269,10 @@ export class DiffViewProvider {
266269 this . openToken = versionTokenOfStat ( preStats )
267270 if ( displayTask && ! displayTask . observationRegistry . has ( absolutePath ) ) {
268271 displayTask . observationRegistry . observe ( absolutePath , this . openToken , false )
269- this . previewRecordedObservation = { path : absolutePath , version : this . openToken }
272+ const recorded = displayTask . observationRegistry . get ( absolutePath )
273+ if ( recorded ) {
274+ this . previewRecordedObservation = { path : absolutePath , entry : recorded }
275+ }
270276 }
271277 }
272278 } else {
@@ -1874,20 +1880,17 @@ export class DiffViewProvider {
18741880 // reaches finalization - tool catch blocks call reset(), and revertChanges
18751881 // arrives through finalizeSession - so this single hook covers the exits
18761882 // that never touch saveChanges' restore. The entry is dropped only while it
1877- // still holds the preview's token AND the preview's incomplete flag: a
1878- // successful publish replaces the token first, and a model read_file records
1879- // complete: true, so earned observations survive.
1883+ // still holds the exact entry object it wrote: any later observe() - a
1884+ // successful publish, a model read (even a partial one at the same token
1885+ // and flag) - replaces the entry with a fresh object, so earned
1886+ // observations survive.
18801887 const previewRecorded = this . previewRecordedObservation
18811888 this . previewRecordedObservation = undefined
18821889 if ( previewRecorded ) {
18831890 const previewTask = this . taskRef . deref ( )
1884- const stillPreview = previewTask ?. observationRegistry . get ( previewRecorded . path )
1885- if (
1886- previewTask &&
1887- stillPreview &&
1888- stillPreview . version === previewRecorded . version &&
1889- ! stillPreview . complete
1890- ) {
1891+ // Ownership by object identity: the stored object still being the one
1892+ // this preview wrote means no read has claimed the entry since.
1893+ if ( previewTask && previewTask . observationRegistry . get ( previewRecorded . path ) === previewRecorded . entry ) {
18911894 previewTask . observationRegistry . forget ( previewRecorded . path )
18921895 }
18931896 }
0 commit comments