Skip to content

Commit 93a83aa

Browse files
Merge 7ea7080 into e487cd7
2 parents e487cd7 + 7ea7080 commit 93a83aa

18 files changed

Lines changed: 4699 additions & 52 deletions

‎knip.json‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@
1616
"playwright-ct": false,
1717
"workspaces": {
1818
"src": {
19-
"entry": ["extension.ts!", "extension/api.ts!", "workers/countTokens.ts!"],
19+
"entry": [
20+
"extension.ts!",
21+
"extension/api.ts!",
22+
"workers/countTokens.ts!",
23+
"services/file-safety/safeWriteText.ts!"
24+
],
2025
"project": [
2126
"**/*.ts!",
2227
"!**/__tests__/**!",
@@ -26,6 +31,7 @@
2631
"!utils/WorkspacePathResolver.ts!",
2732
"!test-utils/**!",
2833
"!utils/versionToken.ts!",
34+
"!core/tools/guardedWrite.ts!",
2935
"!services/tools/Bm25Ranker.ts!",
3036
"!services/tools/types.ts!",
3137
"!core/tools/helpers/toolResultFormatting.ts!"

‎src/core/config/__tests__/importExport.spec.ts‎

Lines changed: 56 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1616,10 +1616,14 @@ describe("importExport", () => {
16161616
expect(mockContextProxy.export).toHaveBeenCalled()
16171617
expect(fs.mkdir).toHaveBeenCalledWith("/mock/path", { recursive: true })
16181618

1619-
expect(safeWriteJson).toHaveBeenCalledWith("/mock/path/zoo-code-settings.json", {
1620-
providerProfiles: mockProviderProfiles,
1621-
globalSettings: mockGlobalSettings,
1622-
})
1619+
expect(safeWriteJson).toHaveBeenCalledWith(
1620+
"/mock/path/zoo-code-settings.json",
1621+
{
1622+
providerProfiles: mockProviderProfiles,
1623+
globalSettings: mockGlobalSettings,
1624+
},
1625+
{ refuseSymlinkTarget: true },
1626+
)
16231627
})
16241628

16251629
it("should include globalSettings when allowedMaxRequests is null", async () => {
@@ -1648,10 +1652,14 @@ describe("importExport", () => {
16481652
contextProxy: mockContextProxy,
16491653
})
16501654

1651-
expect(safeWriteJson).toHaveBeenCalledWith("/mock/path/zoo-code-settings.json", {
1652-
providerProfiles: mockProviderProfiles,
1653-
globalSettings: mockGlobalSettings,
1654-
})
1655+
expect(safeWriteJson).toHaveBeenCalledWith(
1656+
"/mock/path/zoo-code-settings.json",
1657+
{
1658+
providerProfiles: mockProviderProfiles,
1659+
globalSettings: mockGlobalSettings,
1660+
},
1661+
{ refuseSymlinkTarget: true },
1662+
)
16551663
})
16561664

16571665
it("should handle errors during the export process", async () => {
@@ -1771,10 +1779,14 @@ describe("importExport", () => {
17711779
contextProxy: mockContextProxy,
17721780
})
17731781

1774-
expect(safeWriteJson).toHaveBeenCalledWith("/mock/path/zoo-code-settings.json", {
1775-
providerProfiles: mockProviderProfiles,
1776-
globalSettings: mockGlobalSettings,
1777-
})
1782+
expect(safeWriteJson).toHaveBeenCalledWith(
1783+
"/mock/path/zoo-code-settings.json",
1784+
{
1785+
providerProfiles: mockProviderProfiles,
1786+
globalSettings: mockGlobalSettings,
1787+
},
1788+
{ refuseSymlinkTarget: true },
1789+
)
17781790
})
17791791

17801792
it("should export model dimension for OpenAI Compatible provider", async () => {
@@ -1924,10 +1936,14 @@ describe("importExport", () => {
19241936
})
19251937

19261938
// Should not throw an error and should preserve original settings
1927-
expect(safeWriteJson).toHaveBeenCalledWith("/mock/path/zoo-code-settings.json", {
1928-
providerProfiles: mockProviderProfiles,
1929-
globalSettings: mockGlobalSettings, // Should remain unchanged
1930-
})
1939+
expect(safeWriteJson).toHaveBeenCalledWith(
1940+
"/mock/path/zoo-code-settings.json",
1941+
{
1942+
providerProfiles: mockProviderProfiles,
1943+
globalSettings: mockGlobalSettings, // Should remain unchanged
1944+
},
1945+
{ refuseSymlinkTarget: true },
1946+
)
19311947
})
19321948

19331949
it("should maintain backward compatibility with existing exports", async () => {
@@ -1967,10 +1983,14 @@ describe("importExport", () => {
19671983
})
19681984

19691985
// Should not modify settings for non-openai-compatible providers
1970-
expect(safeWriteJson).toHaveBeenCalledWith("/mock/path/zoo-code-settings.json", {
1971-
providerProfiles: mockProviderProfiles,
1972-
globalSettings: mockGlobalSettings, // Should remain unchanged
1973-
})
1986+
expect(safeWriteJson).toHaveBeenCalledWith(
1987+
"/mock/path/zoo-code-settings.json",
1988+
{
1989+
providerProfiles: mockProviderProfiles,
1990+
globalSettings: mockGlobalSettings, // Should remain unchanged
1991+
},
1992+
{ refuseSymlinkTarget: true },
1993+
)
19741994
})
19751995

19761996
it("should handle missing current provider gracefully", async () => {
@@ -2012,10 +2032,14 @@ describe("importExport", () => {
20122032
})
20132033

20142034
// Should not throw an error and should preserve original settings
2015-
expect(safeWriteJson).toHaveBeenCalledWith("/mock/path/zoo-code-settings.json", {
2016-
providerProfiles: mockProviderProfiles,
2017-
globalSettings: mockGlobalSettings, // Should remain unchanged
2018-
})
2035+
expect(safeWriteJson).toHaveBeenCalledWith(
2036+
"/mock/path/zoo-code-settings.json",
2037+
{
2038+
providerProfiles: mockProviderProfiles,
2039+
globalSettings: mockGlobalSettings, // Should remain unchanged
2040+
},
2041+
{ refuseSymlinkTarget: true },
2042+
)
20192043
})
20202044
})
20212045

@@ -2249,10 +2273,14 @@ describe("importExport", () => {
22492273
})
22502274

22512275
// Step 4: Verify the exported data includes the model dimension
2252-
expect(safeWriteJson).toHaveBeenCalledWith("/mock/path/test-settings.json", {
2253-
providerProfiles: mockProviderProfiles,
2254-
globalSettings: mockGlobalSettings,
2255-
})
2276+
expect(safeWriteJson).toHaveBeenCalledWith(
2277+
"/mock/path/test-settings.json",
2278+
{
2279+
providerProfiles: mockProviderProfiles,
2280+
globalSettings: mockGlobalSettings,
2281+
},
2282+
{ refuseSymlinkTarget: true },
2283+
)
22562284

22572285
// Step 5: Get the exported data for import test
22582286
const exportedData = (safeWriteJson as Mock).mock.calls[0][1]

‎src/core/config/importExport.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ export const exportSettings = async ({ providerSettingsManager, contextProxy }:
346346

347347
const dirname = path.dirname(uri.fsPath)
348348
await fs.mkdir(dirname, { recursive: true })
349-
await safeWriteJson(uri.fsPath, { providerProfiles, globalSettings })
349+
await safeWriteJson(uri.fsPath, { providerProfiles, globalSettings }, { refuseSymlinkTarget: true })
350350
} catch (e) {
351351
console.error("Failed to export settings:", e)
352352
// Don't re-throw - the UI will handle showing error messages

‎src/core/task/Task.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ import { ToolRepetitionDetector } from "../tools/ToolRepetitionDetector"
107107
import { restoreTodoListForTask } from "../tools/UpdateTodoListTool"
108108
import { FileContextTracker } from "../context-tracking/FileContextTracker"
109109
import { RooIgnoreController } from "../ignore/RooIgnoreController"
110+
import { ObservationRegistry } from "./observationRegistry"
110111
import { RooProtectedController } from "../protect/RooProtectedController"
111112
import { type AssistantMessageContent, presentAssistantMessage } from "../assistant-message"
112113
import { DispatchState, type PendingWork } from "./dispatchState"
@@ -302,6 +303,7 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
302303
readonly parentTask: Task | undefined = undefined
303304
readonly taskNumber: number
304305
readonly workspacePath: string
306+
readonly observationRegistry = new ObservationRegistry()
305307

306308
/**
307309
* The mode associated with this task. Persisted across sessions
@@ -1462,6 +1464,7 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
14621464
/** Cancels the current persistence generation before creating the next assistant-turn boundary. */
14631465
private resetAssistantMessagePersistence(): void {
14641466
this.cancelAssistantMessagePersistence()
1467+
14651468
this.assistantMessagePersistencePromise = new Promise<AssistantMessagePersistenceResult>((resolve) => {
14661469
this.resolveAssistantMessagePersistence = resolve
14671470
})
@@ -3699,6 +3702,12 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
36993702
this.cancelAssistantMessagePersistence()
37003703
this.cancelPendingPartialMessageUpdate()
37013704

3705+
// Drop the per-task file observations. The registry holds an entry per absolute
3706+
// path the task read or wrote (version token + timestamp); nothing can consume them
3707+
// after disposal, and a long-lived extension host would otherwise keep every path a
3708+
// finished task touched alive.
3709+
this.observationRegistry.close()
3710+
37023711
// Release handler-owned host resources (e.g. VS Code configuration listeners).
37033712
// This is the fallback for leases that never settle; later lease releases
37043713
// find nothing retired and so cannot dispose twice.

‎src/core/task/__tests__/Task.spec.ts‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1497,6 +1497,71 @@ describe("Cline", () => {
14971497
})
14981498
})
14991499

1500+
describe("observation registry lifecycle (S4a, epic #1375)", () => {
1501+
it("gives each Task its own observation registry", () => {
1502+
const firstTask = new Task({
1503+
provider: mockProvider,
1504+
apiConfiguration: mockApiConfig,
1505+
task: "first observation task",
1506+
startTask: false,
1507+
})
1508+
const secondTask = new Task({
1509+
provider: mockProvider,
1510+
apiConfiguration: mockApiConfig,
1511+
task: "second observation task",
1512+
startTask: false,
1513+
})
1514+
1515+
// The guarded-write contract assumes an observation in one task never validates
1516+
// a write issued by another task.
1517+
expect(firstTask.observationRegistry).not.toBe(secondTask.observationRegistry)
1518+
firstTask.observationRegistry.observe("/workspace/a.ts", "v-a")
1519+
expect(firstTask.observationRegistry.get("/workspace/a.ts")?.version).toBe("v-a")
1520+
expect(secondTask.observationRegistry.get("/workspace/a.ts")).toBeUndefined()
1521+
})
1522+
1523+
it("clears the observation registry when the task is disposed", async () => {
1524+
const task = new Task({
1525+
provider: mockProvider,
1526+
apiConfiguration: mockApiConfig,
1527+
task: "disposed observation task",
1528+
startTask: false,
1529+
})
1530+
task.observationRegistry.observe("/workspace/a.ts", "v-a")
1531+
task.observationRegistry.observe("/workspace/b.ts", "v-b")
1532+
1533+
await task.dispose()
1534+
1535+
// A disposed task cannot serve another guarded write, so its observed paths
1536+
// (version token + timestamp each) must not stay reachable for the host lifetime.
1537+
expect(task.observationRegistry.get("/workspace/a.ts")).toBeUndefined()
1538+
expect(task.observationRegistry.get("/workspace/b.ts")).toBeUndefined()
1539+
// Closing is what makes a later observation refuse, so it is asserted rather than
1540+
// left to the test name: clearing the map alone would look identical from here.
1541+
expect(task.observationRegistry.isClosed).toBe(true)
1542+
})
1543+
1544+
it("refuses an observation recorded after the task was disposed", async () => {
1545+
const task = new Task({
1546+
provider: mockProvider,
1547+
apiConfiguration: mockApiConfig,
1548+
task: "late observation task",
1549+
startTask: false,
1550+
})
1551+
1552+
await task.dispose()
1553+
1554+
// A read that was already in flight when the task was disposed can finish late and
1555+
// call observe(). Recording then would repopulate a registry no task owns and hand a
1556+
// version token to a guarded write that will never happen.
1557+
task.observationRegistry.observe("/workspace/late.ts", "v-late")
1558+
1559+
expect(task.observationRegistry.get("/workspace/late.ts")).toBeUndefined()
1560+
expect(task.observationRegistry.size).toBe(0)
1561+
expect(task.observationRegistry.isClosed).toBe(true)
1562+
})
1563+
})
1564+
15001565
describe("create", () => {
15011566
it("rejects missing task inputs before constructing the task", () => {
15021567
const onCreated = vi.fn()
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import { describe, it, expect, vi } from "vitest"
2+
3+
import { ObservationRegistry } from "../observationRegistry"
4+
5+
describe("ObservationRegistry", () => {
6+
it("observe → get returns the recorded version and observedAt", () => {
7+
const reg = new ObservationRegistry()
8+
reg.observe("/a/b/c.ts", "1:2:300:4000000000:5000000000")
9+
10+
const obs = reg.get("/a/b/c.ts")
11+
expect(obs).toBeDefined()
12+
expect(obs!.version).toBe("1:2:300:4000000000:5000000000")
13+
expect(typeof obs!.observedAt).toBe("number")
14+
})
15+
16+
it("re-observe replaces the entry with a fresh observedAt", () => {
17+
vi.useFakeTimers()
18+
const reg = new ObservationRegistry()
19+
reg.observe("/a/b/c.ts", "v1")
20+
const first = reg.get("/a/b/c.ts")!
21+
expect(first.version).toBe("v1")
22+
23+
vi.advanceTimersByTime(50)
24+
reg.observe("/a/b/c.ts", "v2")
25+
const second = reg.get("/a/b/c.ts")!
26+
expect(second.version).toBe("v2")
27+
expect(second.observedAt).toBeGreaterThan(first.observedAt)
28+
29+
vi.useRealTimers()
30+
})
31+
32+
it("has returns true for observed paths, false otherwise", () => {
33+
const reg = new ObservationRegistry()
34+
reg.observe("/x.ts", "t1")
35+
expect(reg.has("/x.ts")).toBe(true)
36+
expect(reg.has("/y.ts")).toBe(false)
37+
})
38+
39+
it("size reflects the number of observed entries", () => {
40+
const reg = new ObservationRegistry()
41+
expect(reg.size).toBe(0)
42+
reg.observe("/a.ts", "t1")
43+
reg.observe("/b.ts", "t2")
44+
expect(reg.size).toBe(2)
45+
})
46+
47+
it("clear removes all entries and resets size to 0", () => {
48+
const reg = new ObservationRegistry()
49+
reg.observe("/a.ts", "t1")
50+
reg.observe("/b.ts", "t2")
51+
reg.clear()
52+
expect(reg.size).toBe(0)
53+
expect(reg.get("/a.ts")).toBeUndefined()
54+
expect(reg.has("/b.ts")).toBe(false)
55+
})
56+
57+
it("get on empty registry returns undefined", () => {
58+
const reg = new ObservationRegistry()
59+
expect(reg.get("/any.ts")).toBeUndefined()
60+
})
61+
62+
it("separate instances are independent — observing in one does not appear in the other", () => {
63+
const regA = new ObservationRegistry()
64+
const regB = new ObservationRegistry()
65+
regA.observe("/shared.ts", "v1")
66+
expect(regA.get("/shared.ts")).toBeDefined()
67+
expect(regB.get("/shared.ts")).toBeUndefined()
68+
regB.observe("/shared.ts", "v2")
69+
expect(regA.get("/shared.ts")!.version).toBe("v1")
70+
expect(regB.get("/shared.ts")!.version).toBe("v2")
71+
})
72+
})
73+
74+
describe("close() - disposal is terminal", () => {
75+
it("drops every observation and refuses later ones", () => {
76+
const registry = new ObservationRegistry()
77+
registry.observe("/workspace/a.ts", "v-a")
78+
expect(registry.size).toBe(1)
79+
80+
registry.close()
81+
82+
expect(registry.size).toBe(0)
83+
expect(registry.isClosed).toBe(true)
84+
85+
// A read that was in flight when the task was disposed must not repopulate it.
86+
registry.observe("/workspace/late.ts", "v-late")
87+
expect(registry.get("/workspace/late.ts")).toBeUndefined()
88+
expect(registry.size).toBe(0)
89+
})
90+
})

0 commit comments

Comments
 (0)