Skip to content

Commit 14f8634

Browse files
fix(editor): withdraw the preview entry by ownership, not content
A model partial read while the diff is open replaces the preview observation with a fresh entry at the SAME token and the SAME incomplete flag - indistinguishable by content - so the version+flag withdrawal condition deleted the model's earned authorization and a later targeted edit needed another read. open() now keeps the exact entry object its observe() wrote; reset() forgets only while the registry still stores that object. Any later observe() - publish, model read, partial or not - replaces the entry with a fresh object, so every earned observation survives. Regression test: model partial read after open() survives reset(). Negative controls, each exactly one red: content-based condition (the pre-fix rule) reddens the new test; disabling the withdrawal reddens the trigger test. 153/153 green.
1 parent a00c7e2 commit 14f8634

2 files changed

Lines changed: 38 additions & 19 deletions

File tree

‎src/integrations/editor/DiffViewProvider.ts‎

Lines changed: 21 additions & 19 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"
@@ -132,13 +133,15 @@ export class DiffViewProvider {
132133
* caller never read, publishing stale content over the intervening change.
133134
*/
134135
private preOpenObservation: { version: string; complete: boolean } | null | undefined = undefined
135-
// The observation this session's preview recorded (path + token), if any.
136-
// reset() withdraws it while the entry still holds that token: the preview is
137-
// the tool's own look, not a read the model made, and on the exit paths that
138-
// never reach saveChanges' restore (rejected update saves, cancelled tools,
139-
// reverts) a leftover entry would authorize a later targeted edit for a file
140-
// the model never read.
141-
private previewRecordedObservation: { path: string; version: string } | undefined = undefined
136+
// The entry object this session's preview wrote (path + the exact stored
137+
// object), if any. reset() withdraws only that object: the preview is the
138+
// tool's own look, not a read the model made, and on the exit paths that never
139+
// reach saveChanges' restore (rejected update saves, cancelled tools, reverts)
140+
// a leftover entry would authorize a later targeted edit for a file the model
141+
// never read. Identity, not version/completeness: a model partial read while
142+
// the diff is open replaces the entry with a fresh object at the same token
143+
// and flag, and that read earned its own authorization.
144+
private previewRecordedObservation: { path: string; entry: FileObservation } | undefined = undefined
142145

143146
constructor(
144147
private cwd: string,
@@ -214,7 +217,10 @@ export class DiffViewProvider {
214217
const displayToken = versionTokenOfStat(preStats)
215218
if (displayToken === versionTokenOfStat(postStats)) {
216219
displayTask.observationRegistry.observe(absolutePath, displayToken, false)
217-
this.previewRecordedObservation = { path: absolutePath, version: displayToken }
220+
const recorded = displayTask.observationRegistry.get(absolutePath)
221+
if (recorded) {
222+
this.previewRecordedObservation = { path: absolutePath, entry: recorded }
223+
}
218224
}
219225
}
220226
} else {
@@ -1661,21 +1667,17 @@ export class DiffViewProvider {
16611667
// Withdraw a preview observation that no save path restored. Every caller
16621668
// reaches reset() - tool catch blocks call it, and revertChanges arrives
16631669
// through finalizeSession - so this single hook covers the exits that never
1664-
// touch saveChanges' restore. The entry is dropped only while it still holds
1665-
// the preview's token AND the preview's incomplete flag: a successful
1666-
// publish replaced the token, and a model read_file records complete: true,
1667-
// so earned observations survive.
1670+
// touch saveChanges' restore. The entry is dropped only while it still
1671+
// holds the exact entry object it wrote: any later observe() - a successful
1672+
// publish, a model read (even a partial one at the same token and flag) -
1673+
// replaces the entry with a fresh object, so earned observations survive.
16681674
const previewRecorded = this.previewRecordedObservation
16691675
this.previewRecordedObservation = undefined
16701676
if (previewRecorded) {
16711677
const previewTask = this.taskRef.deref()
1672-
const stillPreview = previewTask?.observationRegistry.get(previewRecorded.path)
1673-
if (
1674-
previewTask &&
1675-
stillPreview &&
1676-
stillPreview.version === previewRecorded.version &&
1677-
!stillPreview.complete
1678-
) {
1678+
// Ownership by object identity: the stored object still being the one
1679+
// this preview wrote means no read has claimed the entry since.
1680+
if (previewTask && previewTask.observationRegistry.get(previewRecorded.path) === previewRecorded.entry) {
16791681
previewTask.observationRegistry.forget(previewRecorded.path)
16801682
}
16811683
}

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

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

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

0 commit comments

Comments
 (0)