Skip to content

Commit a00c5b7

Browse files
fix(editor): withdraw the preview entry by ownership, not content (port)
Same flaw as the sibling unit's thread, same fix on the copy this unit owns: a model partial read while the diff is open replaces the preview observation with a fresh entry at the same token and flag, and the content-based withdrawal deleted the model's earned authorization. open() keeps the exact entry object its observe() wrote; performFinalReset() forgets only while the registry still stores that object. Regression test ported; negative controls, each exactly one red: content-based condition reddens the new test, disabling the withdrawal reddens the trigger test. 167/167 green.
1 parent 5622f24 commit a00c5b7

2 files changed

Lines changed: 37 additions & 17 deletions

File tree

‎src/integrations/editor/DiffViewProvider.ts‎

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { arePathsEqual, getReadablePath } from "../../utils/path"
1818
import { formatResponse } from "../../core/prompts/responses"
1919
import { diagnosticsToProblemsString, getNewDiagnostics } from "../diagnostics"
2020
import { Task } from "../../core/task/Task"
21+
import type { FileObservation } from "../../core/task/observationRegistry"
2122
import { versionTokenOfStat } from "../../utils/versionToken"
2223
import { withFileLock } from "../../utils/fileLock"
2324
import { 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
}

‎src/integrations/editor/__tests__/DiffViewProvider.spec.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1564,6 +1564,23 @@ describe("DiffViewProvider", () => {
15641564
expect(mockTask.observationRegistry.get(`${mockCwd}/test.txt`)?.complete).toBe(true)
15651565
})
15661566

1567+
it("keeps a model partial read that replaced the preview entry at the same token", async () => {
1568+
// A partial read_file while the diff is open replaces the preview
1569+
// observation with a fresh entry at the SAME token and the SAME
1570+
// incomplete flag - indistinguishable by content, but a different
1571+
// owner. The withdrawal is by entry identity, so the model's earned
1572+
// partial authorization survives the reset and a later targeted edit
1573+
// stays authorized.
1574+
mockTask.observationRegistry.clear()
1575+
await openPreview()
1576+
const previewEntry = mockTask.observationRegistry.get(`${mockCwd}/test.txt`)
1577+
expect(previewEntry).toBeDefined()
1578+
mockTask.observationRegistry.observe(`${mockCwd}/test.txt`, previewEntry!.version, false)
1579+
1580+
await diffViewProvider.reset()
1581+
expect(mockTask.observationRegistry.get(`${mockCwd}/test.txt`)).toBeDefined()
1582+
})
1583+
15671584
it("keeps an entry whose token moved on - the preview no longer owns it", async () => {
15681585
// A landed publish (or a later session) replaced the entry: the version
15691586
// no longer names the preview's token, so the withdrawal leaves it

0 commit comments

Comments
 (0)