Skip to content

Commit 41c0295

Browse files
fix(editor): withdraw the preview observation on every exit path
The preview-observation rule ("the tool's own look must not authorize a write") was applied only on saveChanges(..., "edit"). On every other exit path the partial preview entry stayed in task.observationRegistry: a full-file open() records the preview token with complete:false, a rejected "update" save reloads the buffer without writing (token unchanged, nothing forgets), and the model's next apply_diff is then authorized by an observation the model never earned. open() now records the token its preview wrote; reset() - reached by every tool catch block and by revertChanges 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. Regression tests: the full trigger sequence (preview -> rejected update -> leftover -> reset withdraws), plus the two survival cases that pin the guard's conditions (same-token complete:true from a model read survives; a moved token survives). Negative controls, each exactly one red: withdrawal disabled reddens the trigger test; dropping the incomplete-flag condition reddens the same-token survival test; dropping the token condition reddens the moved-token survival test. Also per the trivial row: the lock-cleanup test now asserts the exact stale-version rejection (the outcome is deterministic - open() observed the placeholder, computeVersionToken answers the peer's token) instead of accepting either guard verdict, and the duplicated toHaveBeenCalledWith in the pre-preview authorization test is deduplicated. 152/152 green, eslint clean, prettier clean, tsc adds nothing beyond the known local donor drift.
1 parent bc43841 commit 41c0295

2 files changed

Lines changed: 78 additions & 4 deletions

File tree

‎src/integrations/editor/DiffViewProvider.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,13 @@ export class DiffViewProvider {
132132
* caller never read, publishing stale content over the intervening change.
133133
*/
134134
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
135142

136143
constructor(
137144
private cwd: string,
@@ -207,6 +214,7 @@ export class DiffViewProvider {
207214
const displayToken = versionTokenOfStat(preStats)
208215
if (displayToken === versionTokenOfStat(postStats)) {
209216
displayTask.observationRegistry.observe(absolutePath, displayToken, false)
217+
this.previewRecordedObservation = { path: absolutePath, version: displayToken }
210218
}
211219
}
212220
} else {
@@ -1649,6 +1657,23 @@ export class DiffViewProvider {
16491657
// A reset closes the session whoever calls it through, so the finalization is claimed
16501658
// here as well: a teardown waiter that arrives afterwards must not run a second one.
16511659
this.sessionFinalizationClaimed = true
1660+
1661+
// Withdraw a preview observation that no save path restored. Every caller
1662+
// reaches reset() - tool catch blocks call it, and revertChanges arrives
1663+
// 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.
1668+
const previewRecorded = this.previewRecordedObservation
1669+
this.previewRecordedObservation = undefined
1670+
if (previewRecorded) {
1671+
const previewTask = this.taskRef.deref()
1672+
const stillPreview = previewTask?.observationRegistry.get(previewRecorded.path)
1673+
if (stillPreview && stillPreview.version === previewRecorded.version && !stillPreview.complete) {
1674+
previewTask.observationRegistry.forget(previewRecorded.path)
1675+
}
1676+
}
16521677
// Dispose touch listeners and cancel any pending deferred scroll BEFORE any
16531678
// async editor manipulation. closeAllDiffViews() awaits tab-close operations,
16541679
// so leaving listeners/timers live across that await could let a stale handler

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

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1524,6 +1524,55 @@ describe("DiffViewProvider", () => {
15241524
expect(mockTask.observationRegistry.has(`${mockCwd}/test.txt`)).toBe(false)
15251525
})
15261526

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

15341583
await diffViewProvider.saveChanges(false, 0, "edit")
15351584
expect(safeWriteText).toHaveBeenCalledWith(`${mockCwd}/test.txt`, Buffer.from("new content"))
1536-
expect(safeWriteText).toHaveBeenCalledWith(`${mockCwd}/test.txt`, Buffer.from("new content"))
15371585
})
15381586

15391587
it("publishes the accepted content in the document's own encoding", async () => {
@@ -1860,9 +1908,10 @@ describe("DiffViewProvider", () => {
18601908

18611909
// The publish is rejected against the peer's token, so the discard cleanup runs.
18621910
vi.mocked(computeVersionToken).mockResolvedValue("peer-token")
1863-
await expect(diffViewProvider.saveChanges(false)).rejects.toThrow(
1864-
/Stale version|not read before this write/,
1865-
)
1911+
// Deterministic: open() observed the placeholder token and
1912+
// computeVersionToken answers the peer's token, so the guard takes the
1913+
// stale-version branch - pin it, not the unobserved-create alternative.
1914+
await expect(diffViewProvider.saveChanges(false)).rejects.toThrow(/^Stale version -- the file changed/)
18661915

18671916
// One acquisition for the guarded publish, one for the cleanup.
18681917
expect(lockKeys).toEqual([`${mockCwd}/test.ts`, `${mockCwd}/test.ts`])

0 commit comments

Comments
 (0)