Skip to content

Commit 82cfbf2

Browse files
fix(editor): withdraw the preview observation on every exit path (port)
Port of the sibling unit's security fix to the copy this unit owns: the preview-observation rule was applied only on saveChanges(..., "edit"). On every other exit path the partial preview entry stayed in task.observationRegistry, so a rejected full-file save left an entry that could authorize the model's next targeted edit for a file the model never read. open() records the token its preview wrote; performFinalReset() - the single execution point behind reset(), which every tool catch reaches and revertChanges reaches through finalizeSession - forgets the entry while it still holds that token AND the preview's incomplete flag. A successful publish replaces the token first, and a model read_file records complete: true, so earned observations survive both ways. Three tests ported: the trigger sequence (preview -> rejected update -> leftover asserted -> finalization withdraws) and the two survival cases pinning the guard's conditions. Negative control: disabling the withdrawal reddens exactly the trigger test (1 failed | 165 passed); the survival mutants are killed by their own tests on the sibling copy's identical logic. 166/166 green, eslint clean, prettier clean, tsc adds nothing beyond the known local donor drift.
1 parent c6b3e86 commit 82cfbf2

2 files changed

Lines changed: 74 additions & 0 deletions

File tree

‎src/integrations/editor/DiffViewProvider.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,13 @@ export class DiffViewProvider {
160160
* caller never read, publishing stale content over the intervening change.
161161
*/
162162
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
167+
// 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
163170
// The on-disk version open() stat-matched, kept so accept-time adoption can
164171
// verify the save was authorized against that exact version.
165172
private openToken: string | undefined = undefined
@@ -259,6 +266,7 @@ export class DiffViewProvider {
259266
this.openToken = versionTokenOfStat(preStats)
260267
if (displayTask && !displayTask.observationRegistry.has(absolutePath)) {
261268
displayTask.observationRegistry.observe(absolutePath, this.openToken, false)
269+
this.previewRecordedObservation = { path: absolutePath, version: this.openToken }
262270
}
263271
}
264272
} else {
@@ -1862,6 +1870,23 @@ export class DiffViewProvider {
18621870

18631871
/** The teardown itself: what one finalization of a session consists of. */
18641872
private async performFinalReset(): Promise<void> {
1873+
// Withdraw a preview observation that no save path restored. Every caller
1874+
// reaches finalization - tool catch blocks call reset(), and revertChanges
1875+
// arrives through finalizeSession - so this single hook covers the exits
1876+
// 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.
1880+
const previewRecorded = this.previewRecordedObservation
1881+
this.previewRecordedObservation = undefined
1882+
if (previewRecorded) {
1883+
const previewTask = this.taskRef.deref()
1884+
const stillPreview = previewTask?.observationRegistry.get(previewRecorded.path)
1885+
if (stillPreview && stillPreview.version === previewRecorded.version && !stillPreview.complete) {
1886+
previewTask.observationRegistry.forget(previewRecorded.path)
1887+
}
1888+
}
1889+
18651890
// Dispose touch listeners and cancel any pending deferred scroll BEFORE any
18661891
// async editor manipulation. closeAllDiffViews() awaits tab-close operations,
18671892
// so leaving listeners/timers live across that await could let a stale handler

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1527,6 +1527,55 @@ describe("DiffViewProvider", () => {
15271527
expect(mockTask.observationRegistry.has(`${mockCwd}/test.txt`)).toBe(false)
15281528
})
15291529

1530+
it("withdraws the preview observation on a rejected update save once reset runs", async () => {
1531+
// The full trigger: the model never read the file, open() records the
1532+
// preview entry (complete: false), and saveChanges(..., "update") is
1533+
// rejected as only-partially-read. The discard cleanup reloads the
1534+
// buffer; nothing wrote, so the token is unchanged and nothing forgets
1535+
// the entry. Finalization - reached by every tool catch and by
1536+
// revertChanges through finalizeSession - must withdraw it, or a later
1537+
// apply_diff is authorized by a look the model never made (the
1538+
// edit-path test above covers only the first attempt).
1539+
mockTask.observationRegistry.clear()
1540+
await openPreview()
1541+
expect(mockTask.observationRegistry.has(`${mockCwd}/test.txt`)).toBe(true)
1542+
1543+
await expect(diffViewProvider.saveChanges(false, 0, "update")).rejects.toThrow(/only partially read/)
1544+
// The rejected update leaves the preview entry in place - the leftover
1545+
// the withdrawal must catch.
1546+
expect(mockTask.observationRegistry.has(`${mockCwd}/test.txt`)).toBe(true)
1547+
1548+
await diffViewProvider.reset()
1549+
expect(mockTask.observationRegistry.has(`${mockCwd}/test.txt`)).toBe(false)
1550+
})
1551+
1552+
it("keeps an observation the model earned at the same token after the preview", async () => {
1553+
// A read_file after the preview records complete: true at the same
1554+
// version token (nothing wrote in between). The withdrawal must not
1555+
// touch it: the model did read the file, and its earned authorization
1556+
// outlives the preview.
1557+
mockTask.observationRegistry.clear()
1558+
await openPreview()
1559+
const previewEntry = mockTask.observationRegistry.get(`${mockCwd}/test.txt`)
1560+
expect(previewEntry).toBeDefined()
1561+
mockTask.observationRegistry.observe(`${mockCwd}/test.txt`, previewEntry!.version, true)
1562+
1563+
await diffViewProvider.reset()
1564+
expect(mockTask.observationRegistry.get(`${mockCwd}/test.txt`)?.complete).toBe(true)
1565+
})
1566+
1567+
it("keeps an entry whose token moved on - the preview no longer owns it", async () => {
1568+
// A landed publish (or a later session) replaced the entry: the version
1569+
// no longer names the preview's token, so the withdrawal leaves it
1570+
// alone even though the flag still reads incomplete.
1571+
mockTask.observationRegistry.clear()
1572+
await openPreview()
1573+
mockTask.observationRegistry.observe(`${mockCwd}/test.txt`, "landed-token", false)
1574+
1575+
await diffViewProvider.reset()
1576+
expect(mockTask.observationRegistry.get(`${mockCwd}/test.txt`)?.version).toBe("landed-token")
1577+
})
1578+
15301579
it("still publishes a targeted edit authorized by a pre-preview observation", async () => {
15311580
// The same preview must not break the legitimate case: the model read the
15321581
// file (partially) and nothing changed before the preview, so the restored

0 commit comments

Comments
 (0)